Union and Intersection of two sorted arrays | Python3 program to find union of two sorted arrays ( Handling Duplicates ) ; Taking max element present in either array ; Finding elements from 1 st array ( non duplicates only ) . Using another array for storing union elements of both arrays Assuming max element present in array is not more than 10 ^ 7 ; First element is always present in final answer ; Incrementing the First element ' s ▁ count ▁ ▁ in ▁ it ' s corresponding index in newtable ; Starting traversing the first array from 1 st index till last ; Checking whether current element is not equal to it 's previous element ; Finding only non common elements from 2 nd array ; By checking whether it 's already  present in newtable or not ; Driver Code
def UnionArray ( arr1 , arr2 ) : NEW_LINE INDENT m = arr1 [ - 1 ] NEW_LINE n = arr2 [ - 1 ] NEW_LINE ans = 0 NEW_LINE if m > n : NEW_LINE INDENT ans = m NEW_LINE DEDENT else : NEW_LINE INDENT ans = n NEW_LINE DEDENT newtable = [ 0 ] * ( ans + 1 ) NEW_LINE print ( arr1 [ 0 ] , end = " ▁ " ) NEW_LINE newtable [ arr1 [ 0 ] ] += 1 NEW_LINE for i in range ( 1 , len ( arr1 ) ) : NEW_LINE INDENT if arr1 [ i ] != arr1 [ i - 1 ] : NEW_LINE INDENT print ( arr1 [ i ] , end = " ▁ " ) NEW_LINE newtable [ arr1 [ i ] ] += 1 NEW_LINE DEDENT DEDENT for j in range ( 0 , len ( arr2 ) ) : NEW_LINE INDENT if newtable [ arr2 [ j ] ] == 0 : NEW_LINE INDENT print ( arr2 [ j ] , end = " ▁ " ) NEW_LINE newtable [ arr2 [ j ] ] += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 1 , 2 , 2 , 2 , 3 ] NEW_LINE arr2 = [ 2 , 3 , 4 , 5 ] NEW_LINE UnionArray ( arr1 , arr2 ) NEW_LINE DEDENT