Union and Intersection of Two Sets in Swift


In this tutorial, you are going to learn how to perform union and intersection operations between two sets in Swift programming with proper examples.

Union and Intersection are two opposite types of set operations. One is for including all the elements of two sets and the other one is for including only the common elements present in both of the sets.

Before you go forward, we recommend you to learn Sets in Swift and then follow this tutorial.

Union of two sets

Suppose, we have two sets X and Y. Then the union of these two sets will include all the elements of X and Y.

In a nutshell, the union of two sets returns a new set that will have all the elements present in both of those two sets.

Below is a figure to demonstrate the union of two sets:

Union of Two Sets
Union of Two Sets

Performing Union of Two Sets in Swift programming

In Swift, we can find the union of two sets using the union() method. Below is the syntax of using the union() method:

set1.union(set2)

Where set1 and set2 are two sets we want to find union for. Here we have to call the union() method to the set and pass another set as the argument to find the union of two sets.

Below is the example where we are finding the union of two sets:

var setX: Set = [1,2,3,4]
var setY: Set = [5,6,7,8]

var setU = setX.union(setY)

print(setU)

Output:

[3, 4, 8, 5, 6, 7, 1, 2]

Note: The output may not be in the same order if you run the code in your machine. This is because sets in Swift are an unordered type collection.

In the above example, we have two sets which are setX and setY. We have created a new set setZ which is the union of setX and setY.

Intersection of Sets

The intersection of two sets is just the opposite of what the union does.

The intersection of two sets creates a new set that will contain the common elements present in both of those sets.

Suppose we have two sets X and Y. Then the intersection of these two sets will generate a new set Z which will contain only the common elements of these sets.

Below is the figure to demonstrate the intersection between two sets:

Intersection of Two Sets
The intersection of Two Sets

Performing Intersection of Two Sets in Swift programming

In Swift programming, we can use the intersection() method to perform the intersection operation between two sets.

Below is the syntax of using the intersection() method:

set1.intersection(set2)

In the above syntax, set1 and set2 are two sets. We are finding the intersection of these two sets.

Below is the example in Swift where we are finding the intersection between two sets setX and setY:

var setX: Set = [1,2,3,4]
var setY: Set = [3,4,5,6]

var setI = setX.intersection(setY)

print(setI)

Output:

[3, 4]

Note: if you run the code, the output may not be in the same order as you can see here as set is unordered collection in Swift.

In our code, we have two sets setX and setY. We use the intersection() method to generate a new set setI which is the intersection of two sets setX and setY.