Examining two string arrays for equivalence

前端 未结 5 1378
无人共我
无人共我 2021-01-21 21:38

Is there a better way to examine whether two string arrays have the same contents than this?

string[] first = new string[]{\"cat\",\"and\",\"mouse\"};
string[] s         


        
5条回答
  •  囚心锁ツ
    2021-01-21 22:07

    This is O(n^2). If the arrays have the same length, sort them, then compare elements in the same position. This is O(n log n).

    Or you can use a hash set or dictionary: insert each word in the first array, then see if every word in the second array is in the set or dictionary. This is O(n) on average.

提交回复
热议问题