Posted on by Kalkicode
Code Hash

Check whether given two sets are disjoint or not

Here given code implementation process.

/*
  Java program
  Check whether given two sets are disjoint or not
*/
import java.util.HashSet;
public class SubSet
{
	//This function are displaying given array elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print(" " + arr[i]);
		}
		System.out.print("\n");
	}
	//  Function which is determine whether element of given sets is disjoint 
	public void isDisjointSet(int[] first, int[] second)
	{
        int s1 = first.length;
        int s2 = second.length;
		// Use to check subset
		HashSet <Integer> result = new HashSet <> ();
		boolean status = true;
		for (int i = 0; i < s1; ++i)
		{
			// Get unique values of first array
			result.add(first[i]);
		}
		System.out.print(" Set A : ");
		display(first, s1);
		System.out.print(" Set B : ");
		display(second, s2);
		for (int i = 0; i < s2 && status == true; ++i)
		{
			if (result.contains(second[i]))
			{
				// When elements are already exist in resultant set A
				status = false;
			}
		}
		if (status == true)
		{
			System.out.print(" Sets are disjoint\n\n");
		}
		else
		{
			System.out.print(" Sets are not disjoint\n\n");
		}
	}
	public static void main(String[] args)
	{
		SubSet task = new SubSet();
		// Define array elements
		int[] set1 = {
			2 , 8 , 0 , 5 , 7 , 11
		};
		int[] set2 = {
			1 , 4 , 9 , 10
		};
		int[] set3 = {
			10 , 6 , 5 , 12 , 15 , 1
		};
		//Test case
		task.isDisjointSet(set1, set2);
		task.isDisjointSet(set1, set3);
	}
}

Output

 Set A :  2 8 0 5 7 11
 Set B :  1 4 9 10
 Sets are disjoint

 Set A :  2 8 0 5 7 11
 Set B :  10 6 5 12 15 1
 Sets are not disjoint
// Include header file
#include <iostream>

#include <set>

using namespace std;
class SubSet
{
	public:
		//This function are displaying given array elements
		void display(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << " " << arr[i];
			}
			cout << "\n";
		}
	//  Function which is determine whether element of given sets is disjoint
	void isDisjointSet(int first[], int second[], int s1, int s2)
	{

		// Use to check subset
		set < int > result;
		bool status = true;
		for (int i = 0; i < s1; ++i)
		{
			result.insert(first[i]);
		}
		cout << " Set A : ";
		this->display(first, s1);
		cout << " Set B : ";
		this->display(second, s2);
		for (int i = 0; i < s2 && status == true; ++i)
		{
			if (result.find(second[i]) != result.end())
			{
				// When elements are already exist in resultant set A
				status = false;
			}
		}
		if (status == true)
		{
			cout << " Sets are disjoint\n\n";
		}
		else
		{
			cout << " Sets are not disjoint\n\n";
		}
	}
};
int main()
{
	SubSet task = SubSet();
	// Define array elements
	int set1[] = {
		2 , 8 , 0 , 5 , 7 , 11
	};
	int set2[] = {
		1 , 4 , 9 , 10
	};
	int set3[] = {
		10 , 6 , 5 , 12 , 15 , 1
	};
	//Get the size of arrays
	int s1 = sizeof(set1) / sizeof(set1[0]);
	int s2 = sizeof(set2) / sizeof(set2[0]);
	int s3 = sizeof(set3) / sizeof(set3[0]);
  	//Test case
	task.isDisjointSet(set1, set2, s1, s2);
	task.isDisjointSet(set1, set3, s1, s3);
	return 0;
}

Output

 Set A :  2 8 0 5 7 11
 Set B :  1 4 9 10
 Sets are disjoint

 Set A :  2 8 0 5 7 11
 Set B :  10 6 5 12 15 1
 Sets are not disjoint
// Include namespace system
using System;
using System.Collections.Generic;
/*
  C# program
  Check whether given two sets are disjoint or not
*/
public class SubSet
{
	//This function are displaying given array elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write(" " + arr[i]);
		}
		Console.Write("\n");
	}
	//  Function which is determine whether element of given sets is disjoint
	public void isDisjointSet(int[] first, int[] second)
	{
		int s1 = first.Length;
		int s2 = second.Length;
		// Use to check subset
		HashSet < int > result = new HashSet <int> ();
		Boolean status = true;
		for (int i = 0; i < s1; ++i)
		{
			result.Add(first[i]);
		}
		Console.Write(" Set A : ");
		display(first, s1);
		Console.Write(" Set B : ");
		display(second, s2);
		for (int i = 0; i < s2 && status == true; ++i)
		{
			if (result.Contains(second[i]))
			{
				// When elements are already exist in resultant set A
				status = false;
			}
		}
		if (status == true)
		{
			Console.Write(" Sets are disjoint\n\n");
		}
		else
		{
			Console.Write(" Sets are not disjoint\n\n");
		}
	}
	public static void Main(String[] args)
	{
		SubSet task = new SubSet();
		// Define array elements
		int[] set1 = {
			2 , 8 , 0 , 5 , 7 , 11
		};
		int[] set2 = {
			1 , 4 , 9 , 10
		};
		int[] set3 = {
			10 , 6 , 5 , 12 , 15 , 1
		};
		//Test case
		task.isDisjointSet(set1, set2);
		task.isDisjointSet(set1, set3);
	}
}

Output

 Set A :  2 8 0 5 7 11
 Set B :  1 4 9 10
 Sets are disjoint

 Set A :  2 8 0 5 7 11
 Set B :  10 6 5 12 15 1
 Sets are not disjoint
<?php
/*
  Php program
  Check whether given two sets are disjoint or not
*/
class SubSet
{
	//This function are displaying given array elements
	public	function display( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo " ". $arr[$i];
		}
		echo "\n";
	}
	//  Function which is determine whether element of given sets is disjoint
	public	function isDisjointSet( & $first, & $second)
	{
		$s1 = count($first);
		$s2 = count($second);
		// Use to check subset
		$result = array();
		$status = true;
		for ($i = 0; $i < $s1; ++$i)
		{
			if (in_array($first[$i], $result,True) == false)
            {
				// Get unique values of first array
				$result[]=$first[$i];
        	}
		}
		echo " Set A : ";
		$this->display($first, $s1);
		echo " Set B : ";
		$this->display($second, $s2);
		for ($i = 0; $i < $s2 && $status == true; ++$i)
		{
			if (in_array($second[$i], $result, True))
			{
				// When elements are already exist in resultant set A
				$status = false;
			}
		}
		if ($status == true)
		{
			echo " Sets are disjoint\n\n";
		}
		else
		{
			echo " Sets are not disjoint\n\n";
		}
	}
}

function main()
{
	$task = new SubSet();
	// Define array elements
	$set1 = array(2, 8, 0, 5, 7, 11);
	$set2 = array(1, 4, 9, 10);
	$set3 = array(10, 6, 5, 12, 15, 1);
	//Test case
	$task->isDisjointSet($set1, $set2);
	$task->isDisjointSet($set1, $set3);
}
main();

Output

 Set A :  2 8 0 5 7 11
 Set B :  1 4 9 10
 Sets are disjoint

 Set A :  2 8 0 5 7 11
 Set B :  10 6 5 12 15 1
 Sets are not disjoint
/*
  Node Js program
  Check whether given two sets are disjoint or not
*/
class SubSet
{
	//This function are displaying given array elements
	display(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
		process.stdout.write("\n");
	}
	//  Function which is determine whether element of given sets is disjoint
	isDisjointSet(first, second)
	{
		var s1 = first.length;
		var s2 = second.length;
		// Use to check subset
		var result = new Set();
		var status = true;
		for (var i = 0; i < s1; ++i)
		{
			result.add(first[i]);
		}
		process.stdout.write(" Set A : ");
		this.display(first, s1);
		process.stdout.write(" Set B : ");
		this.display(second, s2);
		for (var i = 0; i < s2 && status == true; ++i)
		{
			if (result.has(second[i]))
			{
				// When elements are already exist in resultant set A
				status = false;
			}
		}
		if (status == true)
		{
			process.stdout.write(" Sets are disjoint\n\n");
		}
		else
		{
			process.stdout.write(" Sets are not disjoint\n\n");
		}
	}
}

function main()
{
	var task = new SubSet();
	// Define array elements
	var set1 = [2, 8, 0, 5, 7, 11];
	var set2 = [1, 4, 9, 10];
	var set3 = [10, 6, 5, 12, 15, 1];
	//Test case
	task.isDisjointSet(set1, set2);
	task.isDisjointSet(set1, set3);
}
main();

Output

 Set A :  2 8 0 5 7 11
 Set B :  1 4 9 10
 Sets are disjoint

 Set A :  2 8 0 5 7 11
 Set B :  10 6 5 12 15 1
 Sets are not disjoint
#   Python 3 program
#   Check whether given two sets are disjoint or not

class SubSet :
	# This function are displaying given list elements
	def display(self, arr, size) :
		i = 0
		while (i < size) :
			print(" ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#   Function which is determine whether element of given sets is disjoint
	def isDisjointSet(self, first, second) :
		s1 = len(first)
		s2 = len(second)
		#  Use to check subset
		result = set()
		status = True
		i = 0
		while (i < s1) :
			#  Get unique values of first list
			result.add(first[i])
			i += 1
		
		print(" Set A : ", end = "")
		self.display(first, s1)
		print(" Set B : ", end = "")
		self.display(second, s2)
		i = 0
		while (i < s2 and status == True) :
			if (second[i] in result) :
				#  When elements are already exist in resultant set A
				status = False
			
			i += 1
		
		if (status == True) :
			print(" Sets are disjoint\n")
		else :
			print(" Sets are not disjoint\n")
		
	

def main() :
	task = SubSet()
	#  Define list elements
	set1 = [2, 8, 0, 5, 7, 11]
	set2 = [1, 4, 9, 10]
	set3 = [10, 6, 5, 12, 15, 1]
	# Test case
	task.isDisjointSet(set1, set2)
	task.isDisjointSet(set1, set3)

if __name__ == "__main__": main()

Output

 Set A :   2  8  0  5  7  11
 Set B :   1  4  9  10
 Sets are disjoint

 Set A :   2  8  0  5  7  11
 Set B :   10  6  5  12  15  1
 Sets are not disjoint
#   Ruby program
#   Check whether given two sets are disjoint or not

class SubSet 
	# This function are displaying given array elements
	def display(arr, size) 
		i = 0
		while (i < size) 
			print(" ", arr[i])
			i += 1
		end

		print("\n")
	end

	#   Function which is determine whether element of given sets is disjoint
	def isDisjointSet(first, second) 
		s1 = first.length
		s2 = second.length
		#  Use to check subset
		result = []
		status = true
		i = 0
		while (i < s1) 
			result.push(first[i])
			i += 1
		end

		print(" Set A : ")
		self.display(first, s1)
		print(" Set B : ")
		self.display(second, s2)
		i = 0
		while (i < s2 && status == true) 
			if (result.include?(second[i])) 
				#  When elements are already exist in resultant set A
				status = false
			end
			i += 1
		end

		if (status == true) 
			print(" Sets are disjoint\n\n")
		else 
			print(" Sets are not disjoint\n\n")
		end

	end

end

def main() 
	task = SubSet.new()
	#  Define array elements
	set1 = [2, 8, 0, 5, 7, 11]
	set2 = [1, 4, 9, 10]
	set3 = [10, 6, 5, 12, 15, 1]
	# Test case
	task.isDisjointSet(set1, set2)
	task.isDisjointSet(set1, set3)
end

main()

Output

 Set A :  2 8 0 5 7 11
 Set B :  1 4 9 10
 Sets are disjoint

 Set A :  2 8 0 5 7 11
 Set B :  10 6 5 12 15 1
 Sets are not disjoint

import scala.collection.mutable._;
/*
  Scala program
  Check whether given two sets are disjoint or not
*/
class SubSet
{
	//This function are displaying given array elements
	def display(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print(" " + arr(i));
			i += 1;
		}
		print("\n");
	}
	//  Function which is determine whether element of given sets is disjoint
	def isDisjointSet(first: Array[Int], second: Array[Int]): Unit = {
		var s1: Int = first.length;
		var s2: Int = second.length;
		// Use to check subset
		var result: Set[Int] = Set();
		var status: Boolean = true;
		var i: Int = 0;
		while (i < s1)
		{
			result.add(first(i));
			i += 1;
		}
		print(" Set A : ");
		this.display(first, s1);
		print(" Set B : ");
		this.display(second, s2);
		i = 0;
		while (i < s2 && status == true)
		{
			if (result.contains(second(i)))
			{
				// When elements are already exist in resultant set A
				status = false;
			}
			i += 1;
		}
		if (status == true)
		{
			print(" Sets are disjoint\n\n");
		}
		else
		{
			print(" Sets are not disjoint\n\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: SubSet = new SubSet();
		// Define array elements
		var set1: Array[Int] = Array(2, 8, 0, 5, 7, 11);
		var set2: Array[Int] = Array(1, 4, 9, 10);
		var set3: Array[Int] = Array(10, 6, 5, 12, 15, 1);
		//Test case
		task.isDisjointSet(set1, set2);
		task.isDisjointSet(set1, set3);
	}
}

Output

 Set A :  2 8 0 5 7 11
 Set B :  1 4 9 10
 Sets are disjoint

 Set A :  2 8 0 5 7 11
 Set B :  10 6 5 12 15 1
 Sets are not disjoint
/*
  Swift 4 program
  Check whether given two sets are disjoint or not
*/
class SubSet
{
	//This function are displaying given array elements
	func display(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	//  Function which is determine whether element of given sets is disjoint
	func isDisjointSet(_ first: [Int], _ second: [Int])
	{
		let s1: Int = first.count;
		let s2: Int = second.count;
		// Use to check subset
		var result = Set<Int>()
		var status: Bool = true;
		var i: Int = 0;
		while (i < s1)
		{
			result.insert(first[i]);
			i += 1;
		}
		print(" Set A : ", terminator: "");
		self.display(first, s1);
		print(" Set B : ", terminator: "");
		self.display(second, s2);
		i = 0;
		while (i < s2 && status == true)
		{
			if (result.contains(second[i]))
			{
				// When elements are already exist in resultant set A
				status = false;
			}
			i += 1;
		}
		if (status == true)
		{
			print(" Sets are disjoint\n");
		}
		else
		{
			print(" Sets are not disjoint\n");
		}
	}
}
func main()
{
	let task: SubSet = SubSet();
	// Define array elements
	let set1: [Int] = [2, 8, 0, 5, 7, 11];
	let set2: [Int] = [1, 4, 9, 10];
	let set3: [Int] = [10, 6, 5, 12, 15, 1];
	//Test case
	task.isDisjointSet(set1, set2);
	task.isDisjointSet(set1, set3);
}
main();

Output

 Set A :   2  8  0  5  7  11
 Set B :   1  4  9  10
 Sets are disjoint

 Set A :   2  8  0  5  7  11
 Set B :   10  6  5  12  15  1
 Sets are not disjoint
/*
  Kotlin program
  Check whether given two sets are disjoint or not
*/
class SubSet
{
	//This function are displaying given array elements
	fun display(arr: Array < Int > , size: Int): Unit
	{
		var i: Int = 0;
		while (i < size)
		{
			print(" " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	//  Function which is determine whether element of given sets is disjoint
	fun isDisjointSet(first: Array <Int> , second: Array <Int> ): Unit
	{
		var s1: Int = first.count();
		var s2: Int = second.count();
		// Use to check subset
		var result: MutableSet <Int> = mutableSetOf <Int> ();
		var status: Boolean = true;
		var i: Int = 0;
		while (i < s1)
		{
			result.add(first[i]);
			i += 1;
		}
		print(" Set A : ");
		this.display(first, s1);
		print(" Set B : ");
		this.display(second, s2);
		i = 0;
		while (i < s2 && status == true)
		{
			if (result.contains(second[i]))
			{
				// When elements are already exist in resultant set A
				status = false;
			}
			i += 1;
		}
		if (status == true)
		{
			print(" Sets are disjoint\n\n");
		}
		else
		{
			print(" Sets are not disjoint\n\n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: SubSet = SubSet();
	// Define array elements
	var set1: Array < Int > = arrayOf(2, 8, 0, 5, 7, 11);
	var set2: Array < Int > = arrayOf(1, 4, 9, 10);
	var set3: Array < Int > = arrayOf(10, 6, 5, 12, 15, 1);
	//Test case
	task.isDisjointSet(set1, set2);
	task.isDisjointSet(set1, set3);
}

Output

 Set A :  2 8 0 5 7 11
 Set B :  1 4 9 10
 Sets are disjoint

 Set A :  2 8 0 5 7 11
 Set B :  10 6 5 12 15 1
 Sets are not disjoint

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment