c# - Any in-built method to only know if two lists are different? -
i have compare 2 lists , only know if contain same values or not. not intend differences.
what best way achieve in c#?
i know can loop lists want know if theres in-built linq/extenstion method achieve provides better performance. did try except/intersect, don't know if suitable ones achieve this.
update: lists won't have duplicates within them.
how want handle duplicates? example, count { 2, 1, 1 } being same { 1, 2 }? (i'm assuming order irrelevant... otherwise use sequenceequal
.)
assuming care "sets" effectively, here 2 options:
a quick , dirty way:
if (!list1.except(list2).any() && !list2.except(list1).any())
a cleaner way:
var set = new hashset<int>(list1); // adjust case accordingly if (set.setequals(list2)) { // lists equal }
one thing consider: if you're interested in treating them sets, might want use set representation start with, instead of list...
Comments
Post a Comment