maxrena
maxrena

Reputation: 571

I want to compare 2 hash sets and take out the differences

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}

Upvotes: 8

Views: 1706

Answers (2)

TheGeneral
TheGeneral

Reputation: 81583

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

Upvotes: 6

ProgrammingLlama
ProgrammingLlama

Reputation: 38880

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

Upvotes: 6

Related Questions