黄舟2017-04-17 12:02:03
First make sure that both arrays are sorted in the same direction, and the rest is an O(N) algorithm.
PS. The questioner didn't actually make it clear. According to the definition, only "sets" can have "intersections", and "sets" cannot have duplicate elements, but "arrays" do not have this restriction.
PHPz2017-04-17 12:02:03
1. Store an array in hashset, O(N)
2. Traverse another array to determine whether the element is in the set, O(N)
The total algorithm time complexity is O(N)
In @windoze's algorithm, the fastest sorting is O(NlogN), so mine will be faster (if it is unordered)