java - 如何快速找出两个数组的交集,前提是两个数组都是百万级的
怪我咯
怪我咯 2017-04-17 12:00:03
0
3
886

快速找出两个数组的交集,两个数组大小分别是百万级的。
PS :hash算法

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
黄舟

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.

PHPzhong

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)

迷茫

What’s a good way to achieve it in PHP

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!