In C#, HashSet.SymmetricExceptWith(other) keeps the items that are in one of the two collections but not in both. Programmers also call it the "XOR" of two sets. In Venn diagram words: keep both circles, but cut out the middle part where they overlap.
With A = {1, 2, 3} and B = {3, 4, 5}, the call A.SymmetricExceptWith(B) turns A into {1, 2, 4, 5}. The shared 3 is removed, and the items only found in B (4 and 5) are added. The set A is changed in place, and B is untouched.
This is an instance member of HashSet<T> (System.Collections.Generic). Call it on a set variable, such as numbers.SymmetricExceptWith(...). A HashSet<T> is a class, so two variables can share one set. It keeps unique items only and does not promise any order.
Syntax
Call SymmetricExceptWith on the set you want to turn into the "different items" set.
C#
HashSet<int> a = new HashSet<int> { 1, 2, 3 };
HashSet<int> b = new HashSet<int> { 3, 4, 5 };
a.SymmetricExceptWith(b); // a is now {1, 2, 4, 5}
a.SymmetricExceptWith(b); // do it again: a is back to {1, 2, 3}
Call
What it does
a.SymmetricExceptWith(b)
Removes shared items from a and adds the items only found in b.
a.SymmetricExceptWith(a)
Everything is shared, so a becomes empty.
a.SymmetricExceptWith(new int[0])
Nothing to compare, so a stays as it is.
If two sets hold exactly the same items, the result is empty. Repeats inside other are treated as one item. Passing null throws ArgumentNullException.
Library declarations
C#
public void SymmetricExceptWith(IEnumerable<T> other);
Parameters
Parameter
Description
other
Required. The collection to compare with. Items found only here are added to the set, and items found in both are removed from the set. It must not be null.
Return Value
Return Value
Description
void
Nothing is returned. The set you called it on now holds only the items that were in exactly one of the two collections.
C# Examples
Example 1 — Keep the Items That Differ
Apply SymmetricExceptWith to {1, 2, 3} and {3, 4, 5} and check the new set.
C#
using System;
using System.Collections.Generic;
class Program {
static string Show(HashSet<int> set) {
List<int> items = new List<int>(set);
items.Sort();
return "{" + string.Join(", ", items) + "}";
}
static void Main() {
HashSet<int> a = new HashSet<int> { 1, 2, 3 };
HashSet<int> b = new HashSet<int> { 3, 4, 5 };
a.SymmetricExceptWith(b);
Console.WriteLine(Show(a));
Console.WriteLine(a.Count);
Console.WriteLine(a.Contains(3));
}
}
Output
{1, 2, 4, 5}
4
False
How It Works
Compare the circles. 1 and 2 live only in a. 4 and 5 live only in b. The number 3 lives in both.
Shared item goes. The 3 is in both sets, so SymmetricExceptWith removes it from a.
New items come in. The items found only in b (4 and 5) are added to a. That is the part ExceptWith never does.
Result.a holds four items, Count is 4, and Contains(3) is False. Show sorts a copy before printing.
Example 2 — Apply It Twice to Get Back to Start
Run the same call two times with the same second set. The second run undoes the first.
C#
using System;
using System.Collections.Generic;
class Program {
static string Show(HashSet<int> set) {
List<int> items = new List<int>(set);
items.Sort();
return "{" + string.Join(", ", items) + "}";
}
static void Main() {
HashSet<int> a = new HashSet<int> { 1, 2, 3 };
HashSet<int> b = new HashSet<int> { 3, 4, 5 };
a.SymmetricExceptWith(b);
Console.WriteLine(Show(a));
a.SymmetricExceptWith(b);
Console.WriteLine(Show(a));
a.SymmetricExceptWith(a);
Console.WriteLine(a.Count);
}
}
Output
{1, 2, 4, 5}
{1, 2, 3}
0
How It Works
First pass. The shared 3 is dropped, and 4 and 5 are added, giving {1, 2, 4, 5}.
Second pass. Now 4 and 5 are in both sets, so they are removed. The number 3 is only in b, so it is added back.
Back to the start. The set is {1, 2, 3} again. Like switching a light on and off, two calls cancel out.
With itself. Every item is shared with itself, so a.SymmetricExceptWith(a) leaves an empty set and Count is 0.
Example 3 — Find Files Added or Deleted
Compare the old file list with the new one. Anything in only one list was either deleted or created.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
HashSet<string> oldFiles = new HashSet<string> { "a.txt", "b.txt", "c.txt" };
string[] newFiles = { "b.txt", "c.txt", "d.txt", "d.txt" };
HashSet<string> changed = new HashSet<string>(oldFiles);
changed.SymmetricExceptWith(newFiles);
List<string> names = new List<string>(changed);
names.Sort();
Console.WriteLine("Changed: " + string.Join(", ", names));
Console.WriteLine(changed.Count);
Console.WriteLine(oldFiles.Count);
}
}
Output
Changed: a.txt, d.txt
2
3
How It Works
Copy the old list.new HashSet<string>(oldFiles) makes a working set, so oldFiles keeps its three names.
Shared files vanish.b.txt and c.txt are in both lists, so they are not "changed" and are removed from the working set.
Only-in-one files stay or arrive.a.txt was only in the old list. d.txt is only in the new list, and its repeat in the array counts once.
Stable print. The two changed names are sorted before printing. changed.Count is 2 and oldFiles.Count is still 3.
Frequently Asked Questions
It changes the set so it holds only the items that are in exactly one of the two collections. Items found in both are removed, and items found only in the other collection are added.
ExceptWith only removes items. SymmetricExceptWith removes the shared items and also adds the items that exist only in the other collection.
An empty set. Every item is shared, so every item is removed and nothing new is added.
Yes. Repeats in the other collection count as one item, so a repeated name is added only once.
Copy the set first, for example new HashSet<int>(a), and call SymmetricExceptWith on the copy.