Any built-in method to know only if the two lists are different from each other?

I need to compare two lists and only know if they contain the same values ​​or not. I am not going to do anything with the differences.

What would be the best way to achieve this in C #?

I know that I can loop through lists, but I want to know if any LINQ / extenstion built-in method can achieve this, which provides better performance. Tried Except / Intersect, but don’t know if they are the most suitable to achieve this.

Update: Lists will not contain duplicates.

+3
source share
1 answer

? , {2, 1, 1} , {1, 2}? ( , ... SequenceEqual.)

, "" , :

:

if (!list1.Except(list2).Any() && !list2.Except(list1).Any())

:

var set = new HashSet<int>(list1); // Adjust case accordingly
if (set.SetEquals(list2))
{
    // Lists were equal
}

, : , , , ...

+6

Source: https://habr.com/ru/post/1790919/


All Articles