This is same as unique element from two arrays
Method 1) O((m+n)nlogn(m+n))+O(m+n) time complexity
Add both the arrays
Sort the elements
Remove the duplicate
O((m+n)nlogn(m+n))+O(m+n)
Code to remove duplicates
current=1 //Point to second element
result=1 //Point to second element
while(curent
if(c[current]!=c[result-1])
{
c[result]=c[current]
result++;
}
current++;
}
Method 2) Tree method, O(m)+O(nlogm) time complexity
Create a BST for one array
Insert element from another array
OR,
Combine both the array
Create BST with larger and smaller condition w/o caring for same element
O(m)+O(nlogm)
Method 3) Sort, O((m+n)logm) time complexity
Sort the smaller array
Display all the element of smaller array
For each element in larger array. do a binary search in smaller array
If the search returns false, that means the data has not displayed yet, So
Display that element also
Whatever elements has been displayed is the union of two arrays
O(mlogm)+O(nlogm) =O((m+n)logm)
Method 4) HashMap, O(m+n) time complexity
Create a hashmap for one of the array
Put the element from another array by avoiding collision
If there is a collision then avoid displaying the element
So displaying all the element that has not been passed through clash will be the union of both the array

Leave a Reply
You must be logged in to post a comment.