How to check if two given sets are disjoint ? | 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
def areDisjoint ( set1 , set2 , n1 , n2 ) : NEW_LINE INDENT myset = set ( [ ] ) NEW_LINE for i in range ( n1 ) : NEW_LINE INDENT myset . add ( set1 [ i ] ) NEW_LINE DEDENT for i in range ( n2 ) : NEW_LINE INDENT if ( set2 [ i ] in myset ) : NEW_LINE return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT set1 = [ 10 , 5 , 3 , 4 , 6 ] NEW_LINE set2 = [ 8 , 7 , 9 , 3 ] NEW_LINE n1 = len ( set1 ) NEW_LINE n2 = len ( set2 ) NEW_LINE if ( areDisjoint ( set1 , set2 , n1 , n2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT