Examining two string arrays for equivalence

前端 未结 5 1392
无人共我
无人共我 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:06

    You could try Enumerable.Intersect: http://msdn.microsoft.com/en-us/library/bb460136.aspx

    The result of the operation is every element that is common to both arrays. If the length of the result is equal to the length of both arrays, then the two arrays contain the same items.

    Enumerable.Union: http://msdn.microsoft.com/en-us/library/bb341731.aspx would work too; just check that the result of the Union operation has length of zero (meaning there are no elements that are unique to only one array);

    Although I'm not exactly sure how the functions handle duplicates.

提交回复
热议问题