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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:56:49+00:00 2026-06-15T12:56:49+00:00

I have two HashSets – setA and setB. How can we find the complement

  • 0

I have two HashSets – setA and setB.

  1. How can we find the complement of setA and setB?
  2. Is the code for intersection the best way to find intersection?

CODE

 string stringA = "A,B,A,A";
 string stringB = "C,A,B,D";

 HashSet<string> setA = new HashSet<string>(stringA.Split(',').Select(t => t.Trim()));
 HashSet<string> setB = new HashSet<string>(stringB.Split(',').Select(t => t.Trim()));

 //Intersection - Present in A and B
 HashSet<string> intersectedSet = new HashSet<string>( setA.Intersect(setB));

 //Complemenet - Present in A; but not present in B

UPDATE:

Use OrdianlIgnoreCase for ignoring case sensitvity How to use HashSet<string>.Contains() method in case -insensitive mode?

REFERENCE:

  1. What is the difference between HashSet<T> and List<T>?
  2. Intersection of multiple lists with IEnumerable.Intersect()
  3. Comparing two hashsets
  4. Compare two hashsets?
  5. Quickest way to find the complement of two collections in C#
  • 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-15T12:56:50+00:00Added an answer on June 15, 2026 at 12:56 pm

    1 – How can we find the complement of setA and setB?

    Use HashSet<T>.Except Method

    //Complemenet - Present in A; but not present in B
    HashSet<string> ComplemenetSet = new HashSet<string>(setA.Except(setB));
    

    try it with following string.

    string stringA = "A,B,A,E";
    string stringB = "C,A,B,D";
    

    ComplementSet will contain E

    2 – Is the code for intersection the best way to find intersection?

    Probably, YES

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

Sidebar

Related Questions

Let say I have two different hashsets as shown below how can I check
I have two hashsets like this: HashSet<string> log1 = new HashSet<string>(File.ReadLines(log1.txt)); HashSet<string> log2 =
I have a class which has two HashSet<String> collections as private members. Other classes
I have two columns say Main and Sub . (they can be of same
Is there an easy way to get the relative complement of two sets? Perhaps
For simplicity's sake, let's say I have two instances of HashMap<String, String> where they
Let's say I have a class called myclass. In my code I have two
I have two tables, DVD and Contact. A DVD can be rented to a
I have two Maps that contain the same type of Objects: Map<String, TaskJSO> a
Hi i have two string builders s1 and s2. I am assigning orders to

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.