Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to AND a big number of arrays of numbers?

I have 200 arrays of sorted positive integers (some of them have over a million of numbers). I need to find the first number that exists in every array. What would you suggest?

like image 824
yegor256 Avatar asked Jul 17 '12 11:07

yegor256


1 Answers

  • Keep an index on every array.
  • Start with the first number of the first array as reference.
  • Is the first number of the n-th array lower than the reference, increase its index.
  • Is the first number of the n-th array equal to the reference, increase n and proceed with - the next array.
  • Is the first number of the n-th array higher than the reference, use that number as reference and start over.
  • If n == 201, your reference exists in every array.

Edit: a code example:

while n < len(data):
    item = data[n][indices[n]]
    if item < reference:
        indices[n] += 1
    elif item == reference:
        n += 1
    elif item > reference:
        reference = item
        n = 0

print reference
like image 190
Sjoerd Avatar answered Sep 21 '22 12:09

Sjoerd