How to check if two given sets are disjoint in Java?

Hello, developers in this tutorial you will learn how to check if two given sets are disjoint in Java. In this tutorial, you will learn two ways of checking that if the sets are disjoint or not. One is the normal way of checking with for loop and traversing and comparing every element of both the sets. And the second method is done with the help of a popular collection framework Hashing where we use HashSet.

What is a disjoint set?

Basically, disjoint sets are those sets that have no elements in common between different sets. For example – If there are two sets with 5 elements in each. Then you need to check that is there any element that is common in both or not. If no there is no common element then the sets are disjoint else, they are not disjoint sets.

Disjoint sets in Java

Here, disjoint sets are the same as in mathematics. And you can find whether the given sets are disjoint or not by the two methods shown in this post which are:-

  1. Disjoint sets using the normal way of checking with for loop and iterating and comparing every element of both the sets.
  2. Disjoint sets using Hashing where HashSet is used to store the elements of sets in a hash table and then using the “.conatains()” method to check if the elements of the first set elements are present in the second set or not while iterating through it.

1. Check if two given sets are disjoint in Java ( First Approach)

  • First, you need to create a class named ‘IfDisjoint’ and mark it as public. Then you need to create two methods in it. First is the main method and the second is the ‘ifDisjoint’ method which returns a boolean value of true or false.

The Main method :

In the main methods first, you need to create two int arrays of fixed size containing elements by typing the code:

int[] set1 = new int[]{10,20,36,55,80,94};
int[] set2 = new int[]{4,5,25,34,62,70,90};

After that, you need to call the second method that is ‘isDisjoint’ by passing both the arrays in it as arguments. The method isDisjoint will return a boolean value of true or false and then you need to store that in a variable called check of type boolean.

boolean check = isDisjoint(set1,set2);

Then the if condition is checked for the stored value in the variable of check.

If the variable contains true then if block executes the code and prints “Sets Are Disjoint!”.

if(check){ System.out.println("Sets Are Disjoint!"); }

Else if the check variable contains false then else block executes the code and prints “Sets Are Not Disjoint!”.

else{ System.out.println(“Sets Are Not Disjoint!”); }

After this, you need to close the main method with closing curly brackets.

The isDisjoint Method

Here, first, you need to create a method named isDisjoint of boolean because it will return a boolean value and then you need to add two parameters in the parentheses set1 and set2 both of type int array.

After this, you need to write a for loop that will iterate through the elements of set1 by initializing a local variable named i of type int. and then set it to 0 which will be the first condition of the for loop then you need to write the second condition by using the property of array ‘.length'(which will return the exact size of the array)  as set1.length and then simultaneously write i++(to increment the value of i).

 for(int i=0;i<set1.length;i++){

Now with respect to the set1 for loop. here, now you need to write a nested for loop for set2 with the same conditions by just changing the variable name to j and use set2.length to get the size of the array of set2.

 for(int j=0;j<set2.length;j++){ 

Now you need to write the if condition to check both the elements of set1 and set2 are equal by traversing through each and every element of it. If any element of set1 matches with any element of set2 then it will return false. Else if none matches i.e. no element of set1 is in set2 then it will return true; “if(set1[i] == set2[j])”


Java Code For The Above Method

public class IfDisjoint{

public static void main(String args[]){
  int[] set1 = new int[]{10,20,36,55,80,94};
  int[] set2 = new int[]{4,5,25,34,62,70,90};

  boolean check = isDisjoint(set1,set2);

  if(check){
    System.out.println("Sets Are Disjoint!");
  }
  else{
    System.out.println("Sets Are Not Disjoint!");
  }
}

public static boolean isDisjoint(int[] set1, int[] set2){
  for(int i=0;i<set1.length;i++){
    for(int j=0;j<set2.length;j++){
      if(set1[i] == set2[j])
        return false;
        }
  }
    
    return true;
    
}

}

 

OUTPUT:

Sets Are Disjoint!

 

2) Checking if two given sets are disjoint in Java (Second Approach)

First, you need to create a class named ‘IfDisjoint’ and mark it as public. Then you need to create two methods in it. In this second approach, you need to write two methods 1) the main method and 2) isDisjoint method

The Main Method is written the same as the First approach code.

The isDisjoint Method

First, you need to write this method name isDisjoint of type boolean as it will return a boolean value. This method accepts two arguments as parameters that are set1 array of type int and set2 array of type int.

Now you need to create an object of HashSet named tempset by typing the code:

HashSet<Integer> tempset = new HashSet<>();

This will create an empty hash table.

Now you need to add set1 array elements in the HashSet table. So, for that, you need to write a for loop which will traverse through every element of set1 from index zero to the last element and use ‘.add’ property to add elements in tempset.

for(int i = 0;i<set1.length;i++)

{ tempset.add(set1[i]); }

After this, you need to write a for loop that will iterate through the set2 array from index zero to the last index and you need to write an if statement simultaneously after that within which you need to check that if the elements of the set2 are present in the hash table of tempset(tempset contains elements of set1) by using a property called “.contains”. And If the elements are present then return false and if not then return true.                                          for(int i=0;i<set2.length;i++) { if(tempset.contains(set2[i])){


Java Code For The Above Method

import java.util.*;
public class IfDisjoint2{
  public static void main(String args[]){
  int[] set1 = new int[]{10,20,36,55,80,94};
  int[] set2 = new int[]{4,5,15,20,62,70,90};
  
  if(isDisjoint(set1, set2)){
    System.out.println("Sets Are Disjoint");
  }
  else{
  System.out.println("Sets Are Not Disjoint!");
  }
  
  }

  public static boolean isDisjoint(int[] set1, int[] set2){
  
    HashSet<Integer> tempset = new HashSet<>();
  
    for(int i = 0;i<set1.length;i++){
      tempset.add(set1[i]);
      }

    for(int i=0;i<set2.length;i++){
      if(tempset.contains(set2[i])){
        return false;
          }
      }
    return true;

}
}

 

OUTPUT

Sets Are Not Disjoint!

 

 

Also, read: Arrays.fill() in Java with examples

How to traverse through a HashSet in Java

Leave a Reply

Your email address will not be published.