This is probably the least efficient, yet sane way to do this. I recommend using the elements of the shorter array as the keys of a hash. Then iterate over the longer array, checking whether that element has been seen.
Hash lookups are generally O(1) (if there are few to no collisions), so this reduces your complexity from O(m*n) to O(m+n).
In reply to Re: Iterating through Two Arrays. Is there a better use of memory?
by AR
in thread Iterating through Two Arrays. Is there a better use of memory?
by Jeri
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |