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

  • SEARCH
  • Home
  • 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 3310774
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:47:56+00:00 2026-05-17T21:47:56+00:00

Possible Duplicate: Comparing two collections for equality I need to verify if two IEnumerable<T>

  • 0

Possible Duplicate:
Comparing two collections for equality

I need to verify if two IEnumerable<T> lists have the same elements, not necessarily in the same order.

I’m targetting .NET 3.5.

Here are the tests. The question is, how should HasSameElements() be implemented?

var l1 = new[]{1,2,3};
var l2 = new[]{3,1,2};

bool rez1 = l1.HasSameElements(l2);//should be true

var l3 = new[]{1,2,3,2};
var l4 = new[]{3,1,2,2};
bool rez2 = l3.HasSameElements(l4);//should be true

var l5 = new[]{1,2,3,2};
var l6 = new[]{1,2,3};
bool rez3 = l5.HasSameElements(l6);//should be false

Aditional notes:

  • In the examples I’m using IEnumerable, but T could be anything. Should T necessarily implement IComparable ?

  • Enumerable.SequenceEquals() by itself doesn’t work, it expects the same order for the elements.

  • Here’s a template for HasElements:

[just some placeholder text as workaround for Markdown ‘code formatting’ bug]

public static class Extensions {
    public static bool HasElements(this IEnumerable<T> l1, IEnumerable<T> l2){
        throw new NotImplementedException();
    } 
}
  • 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-17T21:47:56+00:00Added an answer on May 17, 2026 at 9:47 pm

    Just build a dictionary mapping each object to the number of times it appears in the sequence and then check that the resulting dictionaries are equal.

    Here:

    static class EnumerableExtensions {
        public static bool HasSameElementsAs<T>(
            this IEnumerable<T> first,
            IEnumerable<T> second
        ) {
            var firstMap = first
                .GroupBy(x => x)
                .ToDictionary(x => x.Key, x => x.Count());
            var secondMap = second
                .GroupBy(x => x)
                .ToDictionary(x => x.Key, x => x.Count());
            return 
                firstMap.Keys.All(x =>
                    secondMap.Keys.Contains(x) && firstMap[x] == secondMap[x]
                ) &&
                secondMap.Keys.All(x =>
                    firstMap.Keys.Contains(x) && secondMap[x] == firstMap[x]
                );
        }
    }
    

    Obviously the repeated code can be refactored out into helper methods but that would just muddle the idea here. You could get fancy and accept an IEqualityComparer for the GroupBy operation. Also, you should productionize the code by adding null guards and what not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible duplicate : comparing-two-arrays I have two NSArray and I'd like to create a
Possible Duplicate: Comparing object properties in c# Let's say I have a POCO: public
Possible Duplicate: Reference: Comparing PHP's print and echo Is there any major and fundamental
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: What is the best way to compare two entity framework entities? I
Possible Duplicate: Python ‘==’ vs ‘is’ comparing strings, ‘is’ fails sometimes, why? In Python,
Possible Duplicate: Compiling C code for .NET I have a bunch of C source
Possible Duplicate: How to detect if JavaScript is disabled? I have a website which
Possible Duplicate: String equality vs equality of location I just got my first assessed

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.