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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:57:55+00:00 2026-06-02T13:57:55+00:00

I have the classes: class SomeClass { public string Name{get;set;} public int SomeInt{get;set;} }

  • 0

I have the classes:

class SomeClass
{
   public string Name{get;set;}
   public int SomeInt{get;set;}
}


class SomeComparison: IEqualityComparer<SomeClass>
{
     public bool Equals(SomeClass s, SomeClass d)
     {
         return s.Name == d.Name;
     }

     public int GetHashCode(SomeClass a)
     {
         return (a.Name.GetHashCode() * 251);
     }
}

I also have two large List<SomeClass> called list1 and list2

before I used to have:

 var q = (from a in list1
         from b in list2
         where a.Name != b.Name
         select a).ToList();

and that took about 1 minute to execute. Now I have:

var q =  list1.Except(list2,new SomeComparison()).ToList();

and that takes less than 1 second!

I will like to understand what does the Except method do. Does the method creates a hash table of each list and then perform the same comparison? If I will be performing a lot of this comparisons should I create a Hashtable instead?


EDIT

Now instead of having lists I have two HashSet<SomeClass> called hashSet1 and hashSet2

when I do:

   var q = (from a in hashSet1
           form b in hashSet2
           where a.Name != b.Name
           select a).ToList();

that still takes a long time… What am I doing wrong?

  • 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-06-02T13:57:57+00:00Added an answer on June 2, 2026 at 1:57 pm

    Your guess was close – the Linq to Objects Except extension method uses a HashSet<T> internally for the second sequence passed in – that allows it to look up elements in O(1) while iterating over the first sequence to filter out elements that are contained in the second sequence, hence the overall effort is O(n+m) where n and m are the length of the input sequences – this is the best you can hope to do since you have to look at each element at least once.

    For a review of how this might be implemented I recommend Jon Skeet’s EduLinq series, here part of it’s implementation of Except and the link to the full chapter:

    private static IEnumerable<TSource> ExceptImpl<TSource>(
        IEnumerable<TSource> first,
        IEnumerable<TSource> second,
        IEqualityComparer<TSource> comparer)
    {
        HashSet<TSource> bannedElements = new HashSet<TSource>(second, comparer);
        foreach (TSource item in first)
        {
            if (bannedElements.Add(item))
            {
                yield return item;
            }
        }
    }
    

    Your first implementation on the other hand will compare each element in the first list to each element in the second list – it is performing a cross product. This will require nm operations so it will run in O(nm) – when n and m become large this becomes prohibitively slow very fast. (Also this solution is wrong as is since it will create duplicate elements).

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

Sidebar

Related Questions

I have two classes class A { public string something { get; set; }
I have these classes: public class MovieExt { public string Title { get; set;
I have 2 classes. class SomeClass { public: int SomeFunction() { return 5; }
Say I have three classes: class X{}; class Y{}; class Both : public X,
Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1();
I have two classes: public class Singleton{ private Singleton(){...} private static class InstanceHolder{ private
I have 2 Hibernate classes in a Spring-driven Application like these: @Entity public class
I have two classes, which reference the third: class Data1 { public Named Xxx
I have some class file.h where public: bool frameSendingFinished; is defined. So in class
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;

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.