Algorithm to tell if two arrays have identical members

前端 未结 16 3015
渐次进展
渐次进展 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 07:03

    Pseudocode :

    A:array
    B:array
    C:hashtable
    
    if A.length != B.length then return false;
    
    foreach objA in A
    {
    H = objA;
    if H is not found in C.Keys then
    C.add(H as key,1 as initial value);
    else
    C.Val[H as key]++;
    }
    
    foreach objB in B
    {
    H = objB;
    if H is not found in C.Keys then
    return false;
    else
    C.Val[H as key]--;
    }
    
    if(C contains non-zero value)
    return false;
    else
    return true;
    

提交回复
热议问题