# How to use the “.intersect()”, “.union()”, “symmetricDifference()” and “subtracting()” operations for sets in Swift?

These operations may sound familiar to you, because most of us have dealt with the Venn Diagram at least once in school.

# Operations in a Venn Diagram

A Venn Diagram is a visual representation of the relationships between different sets. It consists of overlapping circles, where each circle represents a set, and the overlapping region represents the intersection of those sets.

The three main operations associated with Venn Diagrams are Union, Intersection, and Complement.

• Union combines all the elements in the sets being considered
• Intersection identifies the common elements among sets
• Complement identifies the elements that are unique to one set.

In the following we will take a closer look at these and even more operations for sets in Swift

# The .intersect() operation

In Swift, “.intersect()” is a set operation that returns a new set that contains the elements that are common to both the original set and another set or sequence. Here is an example that demonstrates the usage of it:

```let firstSet: Set = [1, 2, 3, 4, 5]
let secondSet: Set = [3, 4, 5, 6, 7]
let intersectionSet = firstSet.intersection(secondSet)

print(intersectionSet)

// Output: [5, 3, 4]```
• In this example, we first define two sets “firstSet” and “secondSet” that contain some elements.
• We then call the “.intersection()” method on “firstSet” and pass “secondSet” as a parameter. The resulting set “intersectionSet” contains the elements that are common to both “firstSet” and “secondSet”, which are 3, 4, and 5.

The “.intersect()” operation also works with sequences that contain elements of the same type as the set. For example, an array can be used as input to “.intersect()”:

```let firstSet: Set = [1, 2, 3, 4, 5]
let array = [3, 4, 5, 6, 7]
let intersectionSet = firstSet.intersection(array)

print(intersectionSet)

// Output: [5, 3, 4]```
• In this case, the “array” contains some elements that are in “firstSet”, and the resulting set “intersectionSet” contains these common elements just like in the previous example.

# The .union() operation

“.union()” is a set operation that returns a new set containing all the elements of both the original set and another set or sequence. Here’s an example that demonstrates the usage of “.union()”:

```let firstSet: Set = [1, 2, 3]
let secondSet: Set = [3, 4, 5]
let unionSet = firstSet.union(secondSet)

print(unionSet)

// Output: [1, 2, 3, 4, 5]```
• In this example, we first define two sets “firstSet” and “secondSet” that contain some elements. We then call the “.union()” method on “firstSet” and pass “secondSet” as a parameter.
• The resulting set “unionSet” contains all the elements of both sets, in this case 1, 2, 3, 4, and 5.

Same as  “.intersect()”, the “.union()” method also works with sequences that contain elements of the same type as the set.

• Note that “.union()” creates a new set and does not modify the original set.

# The .symmetricDifference() operation

“.symmetricDifference()” is a set operation that returns a new set containing the elements that are either in the original set or in another set, but not in both. Here is an example that demonstrates the usage of “.symmetricDifference()”:

```let firstSet: Set = [1, 2, 3]
let secondSet: Set = [3, 4, 5]
let symmetricDiffSet = firstSet.symmetricDifference(secondSet)

print(symmetricDiffSet)

// Output: [1, 2, 4, 5]```
• In this example, we first define two sets “firstSet” and “secondSet” that contain some elements.
• We then call the “.symmetricDifference()” method on “firstSet” and pass “secondSet” as a parameter. The resulting set “symmetricDiffSet” contains the elements that are either in “firstSet” or in “secondSet”, but not in both, which are 1, 2, 4, and 5.

Like “.union()” and “.intersect()”, the “.symmetricDifference()” method also works with sequences that contain elements of the same type as the set.

In this case, the resulting set “symmetricDiffSet” contains the elements that are either in “firstSet” or in “array”, but not in both.

• Note that “.symmetricDifference()” creates a new set and does not modify the original set.

# The .subtracting() operation

The “.subtracting()” operation is used to perform set subtraction, which returns a new set containing the elements of the original set that are not present in another set. Here’s an example that demonstrates the usage of “.subtracting()”:

```let setA: Set = [1, 3, 5, 7, 9]
let setB: Set = [2, 4, 6, 8, 10]
let setC = setA.subtracting(setB)

print(setC)

// Output: [1, 3, 5, 7, 9]```
• In this example, we define two sets “setA” and “setB” that contain some elements.
• We then call the “.subtracting()” method on “setA” and pass “setB” as a parameter.
• The resulting set “setC” contains the elements of “setA” that are not present in “setB”, which are 1, 3, 5, 7, and 9.

Note that also the “.subtracting()” operation returns a new set and does not modify the original set. Guess what, finally also the “.subtracting()” operation works with sequences that contain elements of the same type as the set.