0

If you have two lists:

List<string> list1 = new List<string>() { "ABC", "CDE" };
List<string> list2 = new List<string>() { "ABC" };

list1.Except(list2).Any() 

returns true

but if you do the opposite

list2.Except(list1).Any() 

returns false

I thought list2.Except(list1).Any() should return true as well but I was wrong.

Is there a way to compare two list and check if there is differences in the two lists regardless of which order the items is in each list?

I know that there are several ways to do this. With a for loop or with Linq which also results in a for loop. But I am doing this on millions of lists so speed is key.

Addeladde
  • 777
  • 2
  • 9
  • 28
  • 3
    "ABC", removing both "ABC" and "CDE", leaves nothing. So it should return false. Can you explain why you expected it to return true? –  Jan 09 '20 at 21:23
  • 2
    Does this answer your question? [Difference between two lists](https://stackoverflow.com/questions/5636438/difference-between-two-lists) – CeePlusPlus Jan 09 '20 at 21:23
  • You Can use the linked question, and check if the result is empty. Depending on what your doing, a HashSet is the more practical method here. – CeePlusPlus Jan 09 '20 at 21:24
  • Conversely, having "ABC" and "CDE", and removing "ABC", leaves one element ("CDE"). So `Any()` returns true. –  Jan 09 '20 at 21:25
  • `list1.Except(list2).Any()` => true, but `list2.Except(list1).Any()` => False. – Jawad Jan 09 '20 at 21:29
  • list1.ToHashSet().SymmetricExceptWith(list2).Any() seems to be the way to go. – Addeladde Jan 09 '20 at 21:32
  • Imagine you have lists {"ABC", "DEF"} and {"DEF", "GHI"}. Then the order matters as one will give you "ABC" which is in the first list but not the second and the other way gives you "GHI" which is in the second list but not the first. – juharr Jan 09 '20 at 21:34
  • @Addeladde please [edit] or delete the post. It is very unclear what posts asks for and especially how `list1.ToHashSet().SymmetricExceptWith(list2).Any() ` can be an answer... – Alexei Levenkov Jan 09 '20 at 21:36
  • Have edited the post – Addeladde Jan 09 '20 at 21:51

1 Answers1

1

Sets lets you compare 2 lists:

HashSet<string> list1 = new HashSet<string>() { "ABC", "CDE" };
HashSet<string> list2 = new HashSet<string>() { "ABC" };
bool IsProperSubsetOf = list1.IsProperSubsetOf(list2);
bool IsProperSupersetOf = list1.IsProperSupersetOf(list2);
bool IsSubsetOf = list1.IsSubsetOf(list2);
bool IsSupersetOf = list1.IsSupersetOf(list2);
bool SetEquals = list1.SetEquals(list2);
Chris
  • 377
  • 1
  • 10
  • does not answer the question – Jawad Jan 09 '20 at 21:33
  • The question was this: Is there a way to compare two list and check for differences regardless of which order the items is in each list? – Chris Jan 09 '20 at 21:34
  • 1
    It should be noted that using `HashSets` can change the behavior versus `List` if the original `List` contained duplicate items. If the lists are `{"A", "A", "B"}` and `{"A", "B"}`, the difference between the lists is "A". The hashset solution will say there are no differences. –  Jan 09 '20 at 21:39
  • Nevertheless, with all remarks and side notes a fine alternative. – Stefan Jan 09 '20 at 21:54