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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:03:07+00:00 2026-05-17T03:03:07+00:00

When comparing two key-value dictionary sets in C#: set A and set B, what

  • 0

When comparing two key-value dictionary sets in C#: set A and set B, what is the best way to enumerate keys present in set A but missing from set B and vice-versa?

For example:

A = { 1, 2, 5 }
B = { 2, 3, 5 }

Comparing B with A, missing keys = { 1 } and new keys = { 3 }.

Using Dictionary<...,...> objects, one can enumerating all values in B and test against set A using A.ContainsKey(key);, but it feels like there should be a better way that might involve a sorted set?

  • 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-17T03:03:08+00:00Added an answer on May 17, 2026 at 3:03 am

    I’m aware of two built-in ways of doing set differences.

    1) Enumerable.Except

    Produces the set difference of two sequences by using the default equality comparer to compare values.

    Example:

    IEnumerable<int> a = new int[] { 1, 2, 5 };
    IEnumerable<int> b = new int[] { 2, 3, 5 };
    
    foreach (int x in a.Except(b))
    {
        Console.WriteLine(x);  // prints "1"
    }
    

    2a) HashSet<T>.ExceptWith

    Removes all elements in the specified collection from the current HashSet<T> object.

    HashSet<int> a = new HashSet<int> { 1, 2, 5 };
    HashSet<int> b = new HashSet<int> { 2, 3, 5 };
    
    a.ExceptWith(b);
    
    foreach (int x in a)
    {
        Console.WriteLine(x);  // prints "1"
    }
    

    2b) HashSet<T>.SymmetricExceptWith

    Modifies the current HashSet<T> object to contain only elements that are present either in that object or in the specified collection, but not both.

    HashSet<int> a = new HashSet<int> { 1, 2, 5 };
    HashSet<int> b = new HashSet<int> { 2, 3, 5 };
    
    a.SymmetricExceptWith(b);
    
    foreach (int x in a)
    {
        Console.WriteLine(x);  // prints "1" and "3"
    }
    

    If you need something more performant, you’ll probably need to roll your own collection type.

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

Sidebar

Related Questions

I am comparing two dictionary(dic1 and dic2) with rule that get values from dic2
Hey I have a problem comparing the value of a CGPoint (struct with two
Comparing two codes below,both do the same,but with slighty differences: ALTER procedure [dbo].[SP_USUARIOS_UPDATE] @usu_ds
What is the most efficient/recommended way of comparing two NSDates? I would like to
I'm wondering is there a quicker, more efficent way of comparing two text files
I have an android app. in that I am comparing two strings from the
Possible duplicate : comparing-two-arrays I have two NSArray and I'd like to create a
Possible Duplicate: Comparing Two Arrays Using Perl I am trying to find elements that
I need to add custom validator for comparing two dates - start date and
I am having some trouble comparing two variables, hope someone could help. Basically what

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.