C# – Set operations with Linq

In this article, I’ll explain four set operations – intersection, union, difference, and symmetric difference – and how to perform these operations using Linq methods (such as Intersect()). These methods work on any type that implements IEnumerable – such as lists, arrays, and sets.

Set intersection with Intersect()

The intersection of set A {1,2} and set B {2,3} is {2}. This is because element 2 is contained in both set A and set B. The following Venn diagram illustrates the intersection of these two sets.

Venn diagram showing set intersection

You can use the Linq Intersect() method to get the intersection of two sets. Here’s an example:

using System.Collections.Generic;
using System.Linq;

var setA = new HashSet<int>() { 1, 2 };
var setB = new HashSet<int>() { 2, 3 };

var intersectionSet = setA.Intersect(setB);

Console.WriteLine($"Intersect of A and B: {string.Join(",", intersectionSet)}");
Code language: C# (cs)

This outputs the following:

Intersection of A and B: 2Code language: plaintext (plaintext)

Set union with Union()

The union of set A {1,2} with set B {2,3} is {1,2,3}. The union is all elements contained in either set. The following Venn diagram illustrates the union of these two sets.

Venn diagram showing set union

You can use the Linq Union() method to get the union of two sets. Here’s an example:

using System.Collections.Generic;
using System.Linq;

var setA = new HashSet<int>() { 1, 2 };
var setB = new HashSet<int>() { 2, 3 };

var unionSet = setA.Union(setB);

Console.WriteLine($"Union of A and B: {string.Join(",", unionSet)}");
Code language: C# (cs)

This outputs the following:

Union of A and B: 1,2,3Code language: plaintext (plaintext)

Set difference with Except()

The set difference between set A and set B means getting elements that are only in set A. For example, set A {1,2} – set B {2,3} is {1}. This is because element 1 is in set A but not in set B. The following Venn diagram illustrates the set difference.

Venn diagram showing set difference

You can use the Linq Except() method to calculate the set difference. Here’s an example:

using System.Collections.Generic;
using System.Linq;

var setA = new HashSet<int>() { 1, 2 };
var setB = new HashSet<int>() { 2, 3 };

var elementsInAOnly = setA.Except(setB);

Console.WriteLine($"Elements in A only (A - B): {string.Join(",", elementsInAOnly)}");
Code language: C# (cs)

This outputs the following:

Elements in A only (A - B): 1Code language: plaintext (plaintext)

Symmetric difference between two sets

The symmetric difference between set A {1,2} and set B {2,3} is {1,3}. This gets elements that are only in set A or set B, but not in both sets. The following Venn diagram illustrates the symmetric difference:

Venn diagram showing the symmetric difference between two sets

Symmetric difference is really a combination of other set operations: (A – B) union (B – A). You can calculate the symmetric difference by using Linq methods Except() and Union(), like this:

using System.Linq;

var setA = new HashSet<int>() { 1, 2 };
var setB = new HashSet<int>() { 2, 3 };

//(A - B) union (B - A)
var symmetricDifferenceSet = setA.Except(setB).Union(setB.Except(setA));

Console.WriteLine($"Symmetric difference of A and B: {string.Join(",", symmetricDifferenceSet)}");
Code language: C# (cs)

This outputs the following:

Symmetric difference of A and B: 1,3Code language: plaintext (plaintext)

Note: HashSet has a method called SymmetricExceptWith() that gets the symmetric difference, but it modifies the original set.

Leave a Comment