Skip to main content
Reveal hidden code, fix grammar
Source Link
halfer
  • 20.2k
  • 20
  • 110
  • 207

Or you could use SymmetricExceptWith

Modifies the current HashSetHashSet<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); } } 

Source Code here

Or you could use SymmetricExceptWith

Modifies the current HashSet 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 its just uses

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

Source Code here

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

Source Link
TheGeneral
  • 82k
  • 9
  • 111
  • 151

Or you could use SymmetricExceptWith

Modifies the current HashSet 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 its just uses

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

Source Code here