Home  >  Q&A  >  body text

java - 如何快速找出两个数组的交集,前提是两个数组都是百万级的

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

怪我咯怪我咯2727 days ago890

reply all(3)I'll reply

  • 黄舟

    黄舟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.

    reply
    0
  • PHPz

    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)

    reply
    0
  • 迷茫

    迷茫2017-04-17 12:02:03

    What’s a good way to achieve it in PHP

    reply
    0
  • Cancelreply