303

Yet another list-comparing question.

List<MyType> list1;
List<MyType> list2;

I need to check that they both have the same elements, regardless of their position within the list. Each MyType object may appear multiple times on a list. Is there a built-in function that checks this? What if I guarantee that each element appears only once in a list?

EDIT: Guys thanks for the answers but I forgot to add something, the number of occurrences of each element should be the same on both lists.

1
  • 2
    You may be interested in this post, which shows how to fix the null handling issues of the dictionary-based solution while also improving performance. Commented Jan 3, 2016 at 20:39

9 Answers 9

385

If you want them to be really equal (i.e. the same items and the same number of each item), I think that the simplest solution is to sort before comparing:

Enumerable.SequenceEqual(list1.OrderBy(t => t), list2.OrderBy(t => t))

Edit:

Here is a solution that performs a bit better (about ten times faster), and only requires IEquatable, not IComparable:

public static bool ScrambledEquals<T>(IEnumerable<T> list1, IEnumerable<T> list2) {
  var cnt = new Dictionary<T, int>();
  foreach (T s in list1) {
    if (cnt.ContainsKey(s)) {
      cnt[s]++;
    } else {
      cnt.Add(s, 1);
    }
  }
  foreach (T s in list2) {
    if (cnt.ContainsKey(s)) {
      cnt[s]--;
    } else {
      return false;
    }
  }
  return cnt.Values.All(c => c == 0);
}

Edit 2:

To handle any data type as key (for example nullable types as Frank Tzanabetis pointed out), you can make a version that takes a comparer for the dictionary:

public static bool ScrambledEquals<T>(IEnumerable<T> list1, IEnumerable<T> list2, IEqualityComparer<T> comparer) {
  var cnt = new Dictionary<T, int>(comparer);
  ...
Sign up to request clarification or add additional context in comments.

22 Comments

This is a good answer, I believe it is correct, and it is shorter than mine. My only suggestion is to use SequenceEqual as an extension method. I should also point out that this requires that T be IComparable, whereas the ToLookup version only requires a correct GetHashCode and Equals implementation.
Thanks for the code, but only one problem with using a Dictionary - if T is a nullable type, a null in the passed in collection will cause it to fall over. Also, maybe rename the method to ScrambledEggs as that's how i read it the first time i saw it. Must be hungry...
@Guffa Another small optimization could be to check if cnt[s]>0 when iterating the second list. If not, you can return false right away
I know this is old, but even if you add the IEqualityComparer paramater, if you try to iterate over a null list your going to get a null reference exception. You could add the following to the beginning of the function if (list1 == null && list2 == null) { return true; } if (list1 == null || list2 == null) { return false; }
@TwistedWhisper: If you provide a comparer when you create a dictionary, it will use that when it compares the keys. If the comparer handles a nullable type, you can use the null value as one of the keys in the dictionary. The dictionary doesn't care about the key values, it only cares what the comparer tells it about the key values.
|
63

If you don't care about the number of occurrences, I would approach it like this. Using hash sets will give you better performance than simple iteration.

var set1 = new HashSet<MyType>(list1);
var set2 = new HashSet<MyType>(list2);
return set1.SetEquals(set2);

This will require that you have overridden .GetHashCode() and implemented IEquatable<MyType> on MyType.

6 Comments

@recursive: This will not take into account duplicates, as the OP indicates he needs in his edit. This will, however, work if duplicates can be ignored.
The HashSet<T> also needs you to implement the Equals method. The items are compared against each other, it's not just the hash codes that are compared.
@Guffa: All the other approaches here require Equals also.
@recursive: Yes, but the answer mentiones only GetHashCode, as if Equals would not be needed.
You dont need set2 to be a hashset.. any enumerable would do
|
51

As written, this question is ambigous. The statement:

... they both have the same elements, regardless of their position within the list. Each MyType object may appear multiple times on a list.

does not indicate whether you want to ensure that the two lists have the same set of objects or the same distinct set.

If you want to ensure to collections have exactly the same set of members regardless of order, you can use:

// lists should have same count of items, and set difference must be empty
var areEquivalent = (list1.Count == list2.Count) && !list1.Except(list2).Any();

If you want to ensure two collections have the same distinct set of members (where duplicates in either are ignored), you can use:

// check that [(A-B) Union (B-A)] is empty
var areEquivalent = !list1.Except(list2).Union( list2.Except(list1) ).Any();

Using the set operations (Intersect, Union, Except) is more efficient than using methods like Contains. In my opinion, it also better expresses the expectations of your query.

EDIT: Now that you've clarified your question, I can say that you want to use the first form - since duplicates matter. Here's a simple example to demonstrate that you get the result you want:

var a = new[] {1, 2, 3, 4, 4, 3, 1, 1, 2};
var b = new[] { 4, 3, 2, 3, 1, 1, 1, 4, 2 };

// result below should be true, since the two sets are equivalent...
var areEquivalent = (a.Count() == b.Count()) && !a.Except(b).Any(); 

4 Comments

The first method doesn't work in the following case: a = new[] {1,5,5} and b = new[] {1,1,5}. The collections don't have exactly the same set of members but areEquivalent is set to true.
@Remko Jansen is right, the approach with !a.Except(b).Any() is BUGGY - it will say that a={2,2} and b={1,2} are equal. I wonder how it could take so much votes?
This answer doesn't take into account that the count can be the same and except can match but the list can still be different: { 1,1,2,2,3,3 } != { 1,2,3,4,5,6 } Even performing .Except(...) in the opposite direction doesn't solve the problem: { 1,1,2,3 } != { 1,2,2,3 }
Here you can find my extension methods for collection comparison based on this answer, fixing issues mentioned in comments here: stackoverflow.com/a/67486151/379279
15

In addition to Guffa's answer, you could use this variant to have a more shorthanded notation.

public static bool ScrambledEquals<T>(this IEnumerable<T> list1, IEnumerable<T> list2)
{
  var deletedItems = list1.Except(list2).Any();
  var newItems = list2.Except(list1).Any();
  return !newItems && !deletedItems;          
}

2 Comments

This works, but if you want to make it run as optimal as possible, you might want to use return !(list1.Except(list2).Any()) && !(list2.Except(list1).Any()); (#EDIT: Damn, I can't make it work properly. Curse my newbiness.)
[1, 1, 2] and [2, 2, 1] would return true which is incorrect.
10

Thinking this should do what you want:

list1.All(item => list2.Contains(item)) &&
list2.All(item => list1.Contains(item));

if you want it to be distinct, you could change it to:

list1.All(item => list2.Contains(item)) &&
list1.Distinct().Count() == list1.Count &&
list1.Count == list2.Count

5 Comments

You can still get a false positive. Consider {1, 2, 2} and {1, 1, 2}, they contain the same items, has the same count, but still are not equal.
@Guffa: Good point. I think I got it now, with the list1.Distinct() addition. If all the items are the same, and list 1 is distinct, and they both have the same lengths, then list 2 must also be distinct. Now, {1,2} and {2,1} are considered the same, but {1,2,2} and {1,1,2} are not.
While technically correct, the behavior of Contains() may result in O(N<sup>2</sup>) performance. The set operations (Except, Intersect, Union) perform much better if the number of items large.
@Brian Genisio: Now get a false negative on {1, 2, 2} and {1, 2, 2}...
@Guffa: You should. The lists are not distinct. The second answer assumes that the lists are distinct and contain the same values.
9

This is a slightly difficult problem, which I think reduces to: "Test if two lists are permutations of each other."

I believe the solutions provided by others only indicate whether the 2 lists contain the same unique elements. This is a necessary but insufficient test, for example {1, 1, 2, 3} is not a permutation of {3, 3, 1, 2} although their counts are equal and they contain the same distinct elements.

I believe this should work though, although it's not the most efficient:

static bool ArePermutations<T>(IList<T> list1, IList<T> list2)
{
   if(list1.Count != list2.Count)
         return false;

   var l1 = list1.ToLookup(t => t);
   var l2 = list2.ToLookup(t => t);

   return l1.Count == l2.Count 
       && l1.All(group => l2.Contains(group.Key) && l2[group.Key].Count() == group.Count()); 
}

Comments

3

This worked for me:
If you are comparing two lists of objects depend upon single entity like ID, and you want a third list which matches that condition, then you can do the following:

var list3 = List1.Where(n => !List2.select(n1 => n1.Id).Contains(n.Id));

Refer: MSDN - C# Compare Two lists of objects

Comments

0

I use this method )

public delegate bool CompareValue<in T1, in T2>(T1 val1, T2 val2);

public static bool CompareTwoArrays<T1, T2>(this IEnumerable<T1> array1, IEnumerable<T2> array2, CompareValue<T1, T2> compareValue)
{
    return array1.Select(item1 => array2.Any(item2 => compareValue(item1, item2))).All(search => search)
            && array2.Select(item2 => array1.Any(item1 => compareValue(item1, item2))).All(search => search);
}

Comments

-1

try this!!!

using following code you could compare one or many fields to generate a result list as per your need. result list will contain only modified item(s).

// veriables been used
List<T> diffList = new List<T>();
List<T> gotResultList = new List<T>();



// compare First field within my MyList
gotResultList = MyList1.Where(a => !MyList2.Any(a1 => a1.MyListTField1 == a.MyListTField1)).ToList().Except(gotResultList.Where(a => !MyList2.Any(a1 => a1.MyListTField1 == a.MyListTField1))).ToList();
// Generate result list
diffList.AddRange(gotResultList);

// compare Second field within my MyList
gotResultList = MyList1.Where(a => !MyList2.Any(a1 => a1.MyListTField2 == a.MyListTField2)).ToList().Except(gotResultList.Where(a => !MyList2.Any(a1 => a1.MyListTField2 == a.MyListTField2))).ToList();
// Generate result list
diffList.AddRange(gotResultList);


MessageBox.Show(diffList.Count.ToString);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.