I want to compare 2 hash sets and take out the differences
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
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