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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:33:53+00:00 2026-05-15T00:33:53+00:00

I have two collections a and b . I would like to compute the

  • 0

I have two collections a and b. I would like to compute the set of items in either a or b, but not in both (a logical exclusive or). With LINQ, I can come up with this:

IEnumerable<T> Delta<T>(IEnumerable<T> a, IEnumerable<T> b)
{
    return a.Except (b).Union (b.Except (a));
}

I wonder if there are other more efficient or more compact ways of producing the difference between the two collections.

Edit 1: Jon Skeet posted a first solution which does not preserve the order of the items by relying on a HashSet. I wonder if there are other approaches which would preserve the order of a and b in the output.

  • 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-15T00:33:53+00:00Added an answer on May 15, 2026 at 12:33 am

    Use HashSet<T> directly – it has a SymmetricExceptWith method:

    HashSet<T> data = new HashSet<T>(a);
    data.SymmetricExceptWith(b);
    

    EDIT: If you want to maintain the order, here’s an alternative:

    HashSet<T> data = new HashSet<T>(a);
    data.IntersectWith(b);
    foreach (T t in a.Concat(b))
    {
        if (!data.Contains(t))
        {
            yield return t;
        }
    }
    

    This has the following important differences:

    • Both a and b are iterated over twice. In some cases that could be a very bad thing – you could call ToList on each of them to start with to retain a buffer.
    • If there are duplicates in either a or b, they will be yielded multiple times. If you wanted to avoid this you could keep a set of already-yielded values. At this point, it would be equivalent to:

      a.Concat(b).Except(a.Intersect(b))
      

    That’s still only two set operations instead of the three in your original code though.

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

Sidebar

Related Questions

I have two IEnumerable collections that I would like to union. One selects news
I have two collections which have property Email in both collections. I need to
I often come across a scenario where I have two collections of objects (either
I'm currently experimenting a bit with LINQ. Let's say I have two collections of
Consider I have two collections in MongoDB. One for products with documents like: {'_id':
I have a class which has two HashSet<String> collections as private members. Other classes
I have two tables, and they are using different collations. It is not allowed
I have two collections: List<int> ids; List<User> users; Where User has id, name, etc.
Let's assume we have two collections: List<double> values List<SomePoint> points where SomePoint is a
I would like to have controller GET action that returns a JSON-serialized dictionary. 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.