Vahid
Vahid

Reputation: 5444

Check if two list have the same items

I have two lists of integers, list1 and list2. The elements in the lists are the same, but the order is not important. How can I determine whether these lists are equal, ignoring the order of the elements? Is there a built-in method in C# that can do this, or do I need to write my own method?

var list1 = new List<int> {1,2,3};
var list2 = new List<int> {2,1,3};

Upvotes: 32

Views: 46256

Answers (6)

user13770736
user13770736

Reputation:

It's old but i would modify answer by dcastro, and build in extension:

public static bool CompareList(this List<string> _initialList, List<string> _secondaryList) 
{ 
    var set = new HashSet<string>(_initialList); 
    var equals = set.SetEquals(_secondaryList); 
    return equals ? _initialList.Count() == _secondaryList.Count() : false; 
}

This way You would check both elements within the list with sets, but also count of elements helps you to check against excample given in coments like {0, 1, 1} vs {0 ,1}

Additionaly by extension you can call it multiple times within the code. You could modify it to compare against potential null.

public static bool CompareList(this List<string> _initialList, List<string>? _secondaryList) 
{
    //test if _secondaryList is null while initial is not
    if (_secondaryList == null) return false;

    var set = new HashSet<string>(_initialList); 
    var equals = set.SetEquals(_secondaryList); 
    return equals ? _initialList.Count() == _secondaryList.Count() : false; 
}

Upvotes: 3

Graham Laight
Graham Laight

Reputation: 4840

Use SequenceEqual against the ordered lists - e.g.

bool equal = list1.OrderBy(x => x).ToList().SequenceEqual(list2.OrderBy(x => x).ToList());

Upvotes: 1

dcastro
dcastro

Reputation: 68640

That's what sets (e.g., HashSet<T>) are for. Sets have no defined order, and SetEquals verifies whether the set and another collection contain the same elements.

var set = new HashSet<int>(list1);
var equals = set.SetEquals(list2);

Upvotes: 44

Tim Schmelter
Tim Schmelter

Reputation: 460018

You can use !Except + Any:

bool list1InList2 = !list1.Except(list2).Any();

This checks not if both have the same items but if list1 is contained in list2(ignoring duplicates).

If you want to know if list2 is contained in list1, use:

bool list2InList1 = !list2.Except(list1).Any();

So you had to make both checks if you wanted to ensure that both lists contain the same items.

If you also want to take into account that both lists have the same size, precheck with list1.Count==list2.Count. But this check is not useful if you use a set method(see Harald's comment), it doesn't make much sense to compare the counts if you ignore duplicates afterwards.

In general HashSet<T> has some nice and efficient methods to check if two sequences have the same items(ignoring duplicates), dcastro already showed one.


If you want an efficient solution to determine if two lists contain the same items, same count and not ignoring duplicates but ignoring the order(otherwise use SequenceEquals):

public static bool SequenceEqualsIgnoreOrder<T>(this IEnumerable<T> list1, IEnumerable<T> list2, IEqualityComparer<T> comparer = null)
{
    if(list1 is ICollection<T> ilist1 && list2 is ICollection<T> ilist2 && ilist1.Count != ilist2.Count)
        return false;

    if (comparer == null)
        comparer = EqualityComparer<T>.Default;

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

Usage:

var list1 = new List<int> { 1, 2, 3, 1 };
var list2 = new List<int> { 2, 1, 3, 2 };
bool sameItemsIgnoringOrder = list1.SequenceEqualsIgnoreOrder(list2); 
// false because same count and same items but 1 appaears twice in list1 but once in list2

If the order matters and duplicates count too, use:

bool sameItemsSameOrder = list1.SequenceEqual(list2);

Upvotes: 20

brumScouse
brumScouse

Reputation: 3216

Without using linq.

 private static bool AreListsEqual(List<int> list1, List<int> list2)
 {
        var areListsEqual = true;

        if (list1.Count != list2.Count)
            return false;

        for (var i = 0; i < list1.Count; i++)
        {
            if (list2[i] != list1[i])
            {
                areListsEqual = false;
            }
        }

        return areListsEqual;
 }

Upvotes: 1

Kamlesh Meghwal
Kamlesh Meghwal

Reputation: 4962

You can try Except

var result = list1.Except(list2).ToList();

Except returns those elements in first that do not appear in second

Upvotes: 0

Related Questions