Array - 23: Intersection of Two UnSorted arrays | Find common elements of two UnSorted arrays

preview_player
Показать описание
Solution:
- Take a set & one by one put all values of first array
- Now, iterate 2nd array & check if it present in 1st array, put this value in another new Set (i.e finalSet)
- At last, all unique common elments will be present in finalSet
- Time Complexity: O(n + m) for iterating the both array, where m - first array length, m - second array length

Please check video for more info:

This problem is similar to:
how to do intersection of two unsorted array,
intersection of two unsorted arrays,
print intersection of two unsorted array,
intersection,
two unsorted array,
array,
java tutorial,
coding simplified,
java

CHECK OUT CODING SIMPLIFIED

I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 300+ videos. My account is Partner Verified.

★☆★ VIEW THE BLOG POST: ★☆★

★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★

★☆★ SEND EMAIL At: ★☆★
Рекомендации по теме
Комментарии
Автор

very good solution and very detail analysis

laurawu
Автор

Yes, you are right, basically all the solutions on internet prints the duplicate values/

tusharnain
Автор

Is this solution works also with 3 unsorted arrays? Does it give comlexity O(n)?

fatmabadran
Автор

You dont need to take final set. Once you encounter an element of arr2 in the set, print that element and remove it from the set.

gajendralnmiit
Автор

It will be wrong. If there are 2 same elements in both the set.
ex= {4 4}, {1 4 2 3 5 4}

singhmanish
Автор

What if common elements are repeated in both array. Then we will have to take hashmap and store frequency of each element of first array. while traversing second array will decrease frequency whenever we encounter common element. {1 2 2 3} and {2 2 5 3} intersection would be {2 2 3}

mohitkumar-zcwd
Автор

Thankyou for this solution, but this exceeds time limit on GeeksforGeeks.

sarahdaniel