How to check if two given sets are disjoint ? | C # 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
using System ; using System . Collections . Generic ; public class GFG { public static bool areDisjoint ( int [ ] set1 , int [ ] set2 ) { HashSet < int > set = new HashSet < int > ( ) ; 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 = new int [ ] { 10 , 5 , 3 , 4 , 6 } ; int [ ] set2 = new int [ ] { 8 , 7 , 9 , 3 } ; if ( areDisjoint ( set1 , set2 ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }