How to check if two given sets are disjoint ? | C # program to check if two sets are disjoint ; Returns true if set1 [ ] and set2 [ ] are disjoint , else false ; Sort the given two sets ; Check for same elements using merge like process ; if set1 [ i ] == set2 [ j ] ; Driver code
using System ; public class disjoint1 { Boolean aredisjoint ( int [ ] set1 , int [ ] set2 ) { int i = 0 , j = 0 ; Array . Sort ( set1 ) ; Array . Sort ( set2 ) ; while ( i < set1 . Length && j < set2 . Length ) { if ( set1 [ i ] < set2 [ j ] ) i ++ ; else if ( set1 [ i ] > set2 [ j ] ) j ++ ; else return false ; } return true ; } public static void Main ( String [ ] args ) { disjoint1 dis = new disjoint1 ( ) ; int [ ] set1 = { 12 , 34 , 11 , 9 , 3 } ; int [ ] set2 = { 7 , 2 , 1 , 5 } ; Boolean result = dis . aredisjoint ( set1 , set2 ) ; if ( result ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }