Intersection of two arrays

Given two arrays of signed integers, give an approach to find the intersecting set of the two arrays.

1 comment:

h'spec said...

Solution 1 — O(N), but with extra space: use a hash set, traverse the first array, note down the elements. Traverse the second array, and dont emit the elements already in hash set.

Solution 2 — O(N log(N)) : Sort the arrays and then simply use the logic of merge sort.,