Set Operations – Intersect, Union, Difference, and Symmetric Difference

In the show House, different sets of main characters appeared in different seasons. For example, here are the main characters that starred in seasons 1 and 7:

Characters in Season 1Characters in Season 7
House
Cuddy
Wilson
Foreman
Chase
Cameron
House
Cuddy
Wilson
Foreman
Chase
Taub
Thirteen
Masters

Set operations allow you to answer interesting questions, such as “Which House main characters appeared in both seasons 1 and 7?”. This question is really asking us to perform the intersect set operation. Set operations return subsets of elements from two sets. The intersect set operation would return the following subset of House characters: {House, Cuddy, Wilson, Foreman, Chase}.

In this article, I’m going use these two House sets to explain the four main set operations: intersect, union, difference, and symmetric difference. I’ll illustrate these set operations by using Venn diagrams. Finally, I’ll show how to do these set operations in C#.

First, set operations are always performed on at least two sets. Sets are commonly represented by using Venn diagrams. The two House sets can be represented with the following Venn diagram.

Venn Diagram showing main characters in the show House in seasons 1 and 7

Set Intersect

Definition: An element is in the set intersect if it exists in all of the sets.

A: {1,2,3} B: {2,3,4} Intersect of A and B: {2,3}

In the House sets, the intersect is the subset of main characters that appeared in both seasons 1 and 7.

Venn Diagram showing set intersect. Shows main characters in the House that were in both seasons 1 and 7

In C# you can get the set intersect by using the Linq Intersect() method.

var charactersInSeason1 = new HashSet<string>() { "House", "Cuddy", "Wilson", "Foreman", "Chase", "Cameron" }; var charactersInSeason7 = new HashSet<string>() { "House", "Cuddy", "Wilson", "Foreman", "Chase", "Taub", "Thirteen", "Masters" }; var charactersInBothSeasons = charactersInSeason1.Intersect(charactersInSeason7);

Set Union

Definition: An element is in the set union if it exists in any of the sets.

A: {1,2,3} B: {3,4,5} Union of A and B: {1,2,3,4,5}

In the House sets, the union is all of the characters.

Venn Diagram showing the set union of main characters that appeared in House seasons 1 and 7

In C# you can get the union of two sets by using the Linq Union() method.

var charactersInSeason1 = new HashSet<string>() { "House", "Cuddy", "Wilson", "Foreman", "Chase", "Cameron" }; var charactersInSeason7 = new HashSet<string>() { "House", "Cuddy", "Wilson", "Foreman", "Chase", "Taub", "Thirteen", "Masters" }; var charactersInAnySeason = charactersInSeason1.Union(charactersInSeason7);

Set Difference

Definition: An element is in the set difference if it exists in the left set but not in the right set.

Note: Unlike the other set operations, the set difference is expressed with respect to the left operand. A – B is not the same as B – A.

A: {1,2,3} B: {2,3,4} A - B: {1} B - A: {4}

Using the House sets, the set difference (season 1 – season 7) is asking the question: “Which characters appeared in season 1 but not in season 7?”

Venn Diagram showing the set difference operation. This answers the question: "Which characters appeared in season 1 but not in season 7?"

In C# you can get the set difference by using the Linq Except() method.

var charactersInSeason1 = new HashSet<string>() { "House", "Cuddy", "Wilson", "Foreman", "Chase", "Cameron" }; var charactersInSeason7 = new HashSet<string>() { "House", "Cuddy", "Wilson", "Foreman", "Chase", "Taub", "Thirteen", "Masters" }; var charactersInSeason1ButNotInSeason7 = charactersInSeason1.Except(charactersInSeason7);

Set Symmetric Difference

Definition: An element is in the symmetric difference if it appears in only one of the sets.

A: {1,2,3} B: {2,3,4} Symmetric difference of A and B: {1,4}

Using the House sets, this answers the question: “Which characters only appeared in season 1 or season 7, but not both?”

Venn Diagram showing the symmetric difference - "Which characters only appeared in season 1 or season 7 of House?"

The symmetric difference is a compound operation using the other set operations. There are two ways to get it:

  • (A – B) union (B – A)
  • (A union B) – (A intersect B)

The following code in C# shows these two approaches to get the symmetric difference, using Linq methods.

var charactersInSeason1 = new HashSet<string>() { "House", "Cuddy", "Wilson", "Foreman", "Chase", "Cameron" }; var charactersInSeason7 = new HashSet<string>() { "House", "Cuddy", "Wilson", "Foreman", "Chase", "Taub", "Thirteen", "Masters" }; //(A union B) - (A intersect B) var union = charactersInSeason1.Union(charactersInSeason7); var intersect = charactersInSeason1.Intersect(charactersInSeason7); var symmetricDifference1 = union.Except(intersect); //(A - B) union (B - A) var symmetricDifference2 = charactersInSeason1.Except(charactersInSeason7).Union(charactersInSeason7.Except(charactersInSeason1));

There is a third option: the SymmetricExceptWith() method. The problem with this method is that it mutates the original set, which might not be desirable. One solution to that is to create a copy of the set first, then call SymmetricExceptWith() on the copy. It’s really up to you to determine if you prefer this approach, or if you’d rather use the functional-style Linq methods shown in the code above.

var charactersInSeason1 = new HashSet<string>() { "House", "Cuddy", "Wilson", "Foreman", "Chase", "Cameron" }; var charactersInSeason7 = new HashSet<string>() { "House", "Cuddy", "Wilson", "Foreman", "Chase", "Taub", "Thirteen", "Masters" }; var copy = charactersInSeason1.ToHashSet(); copy.SymmetricExceptWith(charactersInSeason7);

Leave a Comment