How to check if two given sets are disjoint ? | Java program to check if two sets are distinct or not ; This function prints all distinct elements ; Creates an empty hashset ; Traverse the first set and store its elements in hash ; Traverse the second set and check if any element of it is already in hash or not . ; Driver method to test above method
import java . util . * ; class Main { static boolean areDisjoint ( int set1 [ ] , int set2 [ ] ) { HashSet < Integer > set = new HashSet < > ( ) ; for ( int i = 0 ; i < set1 . length ; i ++ ) set . add ( set1 [ i ] ) ; for ( int i = 0 ; i < set2 . length ; i ++ ) if ( set . contains ( set2 [ i ] ) ) return false ; return true ; } public static void main ( String [ ] args ) { int set1 [ ] = { 10 , 5 , 3 , 4 , 6 } ; int set2 [ ] = { 8 , 7 , 9 , 3 } ; if ( areDisjoint ( set1 , set2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }