Find Union and Intersection of two unsorted arrays | Prints intersection of arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] ; Iterate first array ; Iterate second array ; Prints union of arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] ; placeZeroes function ; Function to itreate array ; placeValue function ; Hashing collision happened increment position and do recursive call ; Driver code
def findPosition ( a , b ) : NEW_LINE INDENT v = len ( a ) + len ( b ) ; NEW_LINE ans = [ 0 ] * v ; NEW_LINE zero1 = zero2 = 0 ; NEW_LINE print ( " Intersection ▁ : " , end = " ▁ " ) ; NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT zero1 = iterateArray ( a , v , ans , i ) ; NEW_LINE DEDENT for j in range ( len ( b ) ) : NEW_LINE INDENT zero2 = iterateArray ( b , v , ans , j ) ; NEW_LINE DEDENT zero = zero1 + zero2 ; NEW_LINE placeZeros ( v , ans , zero ) ; NEW_LINE printUnion ( v , ans , zero ) ; NEW_LINE DEDENT def printUnion ( v , ans , zero ) : NEW_LINE INDENT zero1 = 0 ; NEW_LINE print ( " Union : " , end = "   " ) ; NEW_LINE for i in range ( v ) : NEW_LINE INDENT if ( ( zero == 0 and ans [ i ] == 0 ) or ( ans [ i ] == 0 and zero1 > 0 ) ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT if ( ans [ i ] == 0 ) : NEW_LINE INDENT zero1 += 1 ; NEW_LINE DEDENT print ( ans [ i ] , end = " , " ) ; NEW_LINE DEDENT DEDENT def placeZeros ( v , ans , zero ) : NEW_LINE INDENT if ( zero == 2 ) : NEW_LINE INDENT print ( "0" ) ; NEW_LINE d = [ 0 ] ; NEW_LINE placeValue ( d , ans , 0 , 0 , v ) ; NEW_LINE DEDENT if ( zero == 1 ) : NEW_LINE INDENT d = [ 0 ] ; NEW_LINE placeValue ( d , ans , 0 , 0 , v ) ; NEW_LINE DEDENT DEDENT def iterateArray ( a , v , ans , i ) : NEW_LINE INDENT if ( a [ i ] != 0 ) : NEW_LINE INDENT p = a [ i ] % v ; NEW_LINE placeValue ( a , ans , i , p , v ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT def placeValue ( a , ans , i , p , v ) : NEW_LINE INDENT p = p % v ; NEW_LINE if ( ans [ p ] == 0 ) : NEW_LINE INDENT ans [ p ] = a [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( ans [ p ] == a [ i ] ) : NEW_LINE INDENT print ( a [ i ] , end = " , " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT p = p + 1 ; NEW_LINE placeValue ( a , ans , i , p , v ) ; NEW_LINE DEDENT DEDENT DEDENT a = [ 7 , 1 , 5 , 2 , 3 , 6 ] ; NEW_LINE b = [ 3 , 8 , 6 , 20 , 7 ] ; NEW_LINE findPosition ( a , b ) ; NEW_LINE