Algorithm to tell if two arrays have identical members

前端 未结 16 3004
渐次进展
渐次进展 2020-11-29 06:52

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 n

16条回答
  •  执笔经年
    2020-11-29 06:59

    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.

提交回复
热议问题