Algorithm to tell if two arrays have identical members

Obvious answers would be:

  1. Sort both lists, then check each
    element to see if they’re identical
  2. Add the items from one array to a
    hashtable, then iterate through the
    other array, checking that each item
    is in the hash
  3. 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.

Leave a Comment