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 4117912
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:53:37+00:00 2026-05-20T22:53:37+00:00

I have a HashSet that contains multiple lists of integers – i.e. HashSet<List<int>> In

  • 0

I have a HashSet that contains multiple lists of integers – i.e. HashSet<List<int>>

In order to maintain uniqueness I am currently having to do two things:
1. Manually loop though existing lists, looking for duplicates using SequenceEquals.
2. Sorting the individual lists so that SequenceEquals works currently.

Is there a better way to do this? Is there an existing IEqualityComparer that I can provide to the HashSet so that HashSet.Add() can automatically handle uniqueness?

var hashSet = new HashSet<List<int>>();

for(/* some condition */)
{
    List<int> list = new List<int>();

    ...

    /* for eliminating duplicate lists */

    list.Sort();

    foreach(var set in hashSet)
    {
        if (list.SequenceEqual(set))
        {
            validPartition = false;
            break;
        }
    }

    if (validPartition)
           newHashSet.Add(list);
}
  • 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-20T22:53:38+00:00Added an answer on May 20, 2026 at 10:53 pm

    Here is a possible comparer that compares an IEnumerable<T> by its elements. You still need to sort manually before adding.

    One could build the sorting into the comparer, but I don’t think that’s a wise choice. Adding a canonical form of the list seems wiser.

    This code will only work in .net 4 since it takes advantage of generic variance. If you need earlier versions you need to either replace IEnumerable with List, or add a second generic parameter for the collection type.

    class SequenceComparer<T>:IEqualityComparer<IEnumerable<T>>
    {
        public bool Equals(IEnumerable<T> seq1,IEnumerable<T> seq2)
        {
            return seq1.SequenceEqual(seq2);
        }
        
        public int GetHashCode(IEnumerable<T> seq)
        {
            int hash = 1234567;
            foreach(T elem in seq)
                hash = unchecked(hash * 37 + elem.GetHashCode());
            return hash;
        }
    }
    
    void Main()
    {
        var hashSet = new HashSet<List<int>>(new SequenceComparer<int>());
    
        List<int> test=new int[]{1,3,2}.ToList();
        test.Sort();
        hashSet.Add(test);
    
        List<int> test2=new int[]{3,2,1}.ToList();
        test2.Sort();       
        hashSet.Contains(test2).Dump();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this list: private List<Set<Address>> scanList; So my list contains multiple scans as
I have a simple little code fragment that is frustrating me: HashSet<long> groupUIDs =
I have an interesting problem, which is a function that returns a Dictionary<String,HashSet<String>> .
I have a custom closed-hashset/open-addressing (i.e. no linked lists) class. It's very specific to
I have a HashSet<string> that is instantiated using StringComparer.CurrentCultureIgnoreCase and am making extensive use
I ran across an assertion that HashSet<T>.Contains() is an O(1) operation. This surprised me
In some library code, I have a List that can contain 50,000 items or
I have a text file that contains very long lines. I need one piece
I have a class that contains a collection. The two instances of the class
Imagine that a library contains many books that have many pages. I've got the

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.