8

I have 2 hash sets like this.

Hash_1 = {1, 2, 3, 4, 5} Hash_2 = {4, 5, 6, 7, 8} 

I am using C#

I want to compare those two sets and want to get the output like

Hash_3 = {1, 2, 3, 6, 7, 8} 

2 Answers 2

6

What you want is: Hash_1 without Hash_2, and Hash_2 without Hash_1, then combined into one set.

So let's start with Hash_1 without Hash_2:

var modified1 = Hash_1.Except(Hash_2); 

and then Hash_2 without Hash_1:

var modified2 = Hash_2.Except(Hash_1); 

And now let's combine them:

var result = modified1.Concat(modified2); 

Or in short:

var result = Hash_1.Except(Hash_2).Concat(Hash_2.Except(Hash_1)); 

Try it online

Sign up to request clarification or add additional context in comments.

Comments

6

Or you could use 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.

var h1 = new HashSet<int>() { 1, 2, 3, 4, 5 }; var h2 = new HashSet<int>() { 4, 5, 6, 7, 8 }; h1.SymmetricExceptWith(h2); Console.WriteLine(string.Join(",", h1)); 

Output

1,2,3,7,6,8 

Internally it just uses

foreach (T item in other) { if (!Remove(item)) { AddIfNotPresent(item); } } 

Source Code here

2 Comments

Oooh. Fancy. I like it!
@John yeah never used it before actually, that was my first rodeo

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.