Or you could use SymmetricExceptWith
Modifies the current HashSet
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 itsit just uses
foreach (T item in other) { if (!Remove(item)) { AddIfNotPresent(item); } }