Algorithm to tell if two arrays have identical members
algorithm
Solution
Obvious answers would be:
- Sort both lists, then check each element to see if they're identical
- Add the items from one array to a hashtable, then iterate through the other array, checking that each item is in the hash
- nickf's iterative search algorithm
Which one you'd use would depend on whether you can sort the lists first, and whether you have a good hash algorithm handy.
Problem
What's the best algorithm for comparing two arrays to see if they have the same members? Assume there are no duplicates, the members can be in any order, and that neither is sorted. ``` compare( [a, b, c, d], [b, a, d, c] ) ==> true compare( [a, b, e], [a, b, c] ) ==> false compare( [a, b, c], [a, b] ) ==> false ```