Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1086215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:47:06+00:00 2026-05-16T22:47:06+00:00

I would like to compare two collections (in C#), but I’m not sure of

  • 0

I would like to compare two collections (in C#), but I’m not sure of the best way to implement this efficiently.

I’ve read the other thread about Enumerable.SequenceEqual, but it’s not exactly what I’m looking for.

In my case, two collections would be equal if they both contain the same items (no matter the order).

Example:

collection1 = {1, 2, 3, 4};
collection2 = {2, 4, 1, 3};

collection1 == collection2; // true

What I usually do is to loop through each item of one collection and see if it exists in the other collection, then loop through each item of the other collection and see if it exists in the first collection. (I start by comparing the lengths).

if (collection1.Count != collection2.Count)
    return false; // the collections are not equal

foreach (Item item in collection1)
{
    if (!collection2.Contains(item))
        return false; // the collections are not equal
}

foreach (Item item in collection2)
{
    if (!collection1.Contains(item))
        return false; // the collections are not equal
}

return true; // the collections are equal

However, this is not entirely correct, and it’s probably not the most efficient way to do compare two collections for equality.

An example I can think of that would be wrong is:

collection1 = {1, 2, 3, 3, 4}
collection2 = {1, 2, 2, 3, 4}

Which would be equal with my implementation. Should I just count the number of times each item is found and make sure the counts are equal in both collections?


The examples are in some sort of C# (let’s call it pseudo-C#), but give your answer in whatever language you wish, it does not matter.

Note: I used integers in the examples for simplicity, but I want to be able to use reference-type objects too (they do not behave correctly as keys because only the reference of the object is compared, not the content).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:47:06+00:00Added an answer on May 16, 2026 at 10:47 pm

    It turns out Microsoft already has this covered in its testing framework: CollectionAssert.AreEquivalent

    Remarks

    Two collections are equivalent if they
    have the same elements in the same
    quantity, but in any order. Elements
    are equal if their values are equal,
    not if they refer to the same object.

    Using reflector, I modified the code behind AreEquivalent() to create a corresponding equality comparer. It is more complete than existing answers, since it takes nulls into account, implements IEqualityComparer and has some efficiency and edge case checks. plus, it’s Microsoft 🙂

    public class MultiSetComparer<T> : IEqualityComparer<IEnumerable<T>>
    {
        private readonly IEqualityComparer<T> m_comparer;
        public MultiSetComparer(IEqualityComparer<T> comparer = null)
        {
            m_comparer = comparer ?? EqualityComparer<T>.Default;
        }
    
        public bool Equals(IEnumerable<T> first, IEnumerable<T> second)
        {
            if (first == null)
                return second == null;
    
            if (second == null)
                return false;
    
            if (ReferenceEquals(first, second))
                return true;
    
            if (first is ICollection<T> firstCollection && second is ICollection<T> secondCollection)
            {
                if (firstCollection.Count != secondCollection.Count)
                    return false;
    
                if (firstCollection.Count == 0)
                    return true;
            }
    
            return !HaveMismatchedElement(first, second);
        }
    
        private bool HaveMismatchedElement(IEnumerable<T> first, IEnumerable<T> second)
        {
            int firstNullCount;
            int secondNullCount;
    
            var firstElementCounts = GetElementCounts(first, out firstNullCount);
            var secondElementCounts = GetElementCounts(second, out secondNullCount);
    
            if (firstNullCount != secondNullCount || firstElementCounts.Count != secondElementCounts.Count)
                return true;
    
            foreach (var kvp in firstElementCounts)
            {
                var firstElementCount = kvp.Value;
                int secondElementCount;
                secondElementCounts.TryGetValue(kvp.Key, out secondElementCount);
    
                if (firstElementCount != secondElementCount)
                    return true;
            }
    
            return false;
        }
    
        private Dictionary<T, int> GetElementCounts(IEnumerable<T> enumerable, out int nullCount)
        {
            var dictionary = new Dictionary<T, int>(m_comparer);
            nullCount = 0;
    
            foreach (T element in enumerable)
            {
                if (element == null)
                {
                    nullCount++;
                }
                else
                {
                    int num;
                    dictionary.TryGetValue(element, out num);
                    num++;
                    dictionary[element] = num;
                }
            }
    
            return dictionary;
        }
    
        public int GetHashCode(IEnumerable<T> enumerable)
        {
            if (enumerable == null) throw new 
                ArgumentNullException(nameof(enumerable));
    
            int hash = 17;
    
            foreach (T val in enumerable)
                hash ^= (val == null ? 42 : m_comparer.GetHashCode(val));
    
            return hash;
        }
    }
    

    Sample usage:

    var set = new HashSet<IEnumerable<int>>(new[] {new[]{1,2,3}}, new MultiSetComparer<int>());
    Console.WriteLine(set.Contains(new [] {3,2,1})); //true
    Console.WriteLine(set.Contains(new [] {1, 2, 3, 3})); //false
    

    Or if you just want to compare two collections directly:

    var comp = new MultiSetComparer<string>();
    Console.WriteLine(comp.Equals(new[] {"a","b","c"}, new[] {"a","c","b"})); //true
    Console.WriteLine(comp.Equals(new[] {"a","b","c"}, new[] {"a","b"})); //false
    

    Finally, you can use your an equality comparer of your choice:

    var strcomp = new MultiSetComparer<string>(StringComparer.OrdinalIgnoreCase);
    Console.WriteLine(strcomp.Equals(new[] {"a", "b"}, new []{"B", "A"})); //true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to compare difference between two dates, but with the code below
I would like to compare two classes that look like this public class Order
I would like to compare two columns in the same table. I want to
I would like to be able to compare two classes derived from the same
I would like to know if there is anyway I can compare two columns
I have two images which i would like to compare and do different operations
I have two arrays that I would like to compare and ultimately wind up
I'm looking to compare two varchars in SQL, one would be something like Cafe
I have two IEnumerable collections that I would like to union. One selects news
Not quite sure if it is ok to do this but, my question is:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.