Swift Set - Subsets, Supersets, and Disjoint Sets

Here we will explain how to check whether two Swift Sets are equal, subsets, supersets, or disjoint.


Checking Equality of Two Sets with ==

You can check if two Swift Sets are equal by using the == operator.

The order of the elements in the Sets does not matter when checking equality.

let set1: Set = [1, 2, 3, 4, 5, 6, 7, 8]
let set2: Set = [3, 6, 9, 12]
let set3: Set = [4, 7, 6, 5, 8, 2, 1, 3]

print(set1 == set2)
print(set1 == set3)

The output will be as follows. set1 and set2 have different elements, so it prints false. set1 and set3 have the same elements, so it prints true.

false
true

Swift Set – Subset, Superset, Disjoint 1


Checking if a Set is a Subset with isSubset()

By using the isSubset() method, you can check if a Swift Set is a subset of another Set.

The isSubset() method returns true even if both Sets are equal. If you want it to return false when both Sets are equal, use the isStrictSubset() method.


Let's use isSubset() and isStrictSubset() to check if set1 is a subset of other Sets.

let set1: Set = [1, 2, 3, 4]
let set2: Set = [4, 2, 3, 1]
let set3: Set = [1, 2, 3, 4, 5]
let set4: Set = [2, 3, 4, 5, 6]

print(set1.isSubset(of: set2))
print(set1.isStrictSubset(of: set2))
print(set1.isSubset(of: set3))
print(set1.isSubset(of: set4))

The output will be as follows. isSubset() returns true if set1 is equal to or a subset of the argument Set. Since set1 and set2 are equal, isStrictSubset() returns false.

true
false
true
false

Checking if a Set is a Superset with isSuperset()

By using the isSuperset() method, you can check if a Swift Set is a superset of another Set.

The isSuperset() method returns true even if both Sets are equal. If you want it to return false when both Sets are equal, use the isStrictSuperset() method.


Let's use isSuperset() and isStrictSuperset() to check if set1 is a superset of other Sets.

let set1: Set = [1, 2, 3, 4, 5]
let set2: Set = [5, 2, 4, 3, 1]
let set3: Set = [1, 2, 3, 4]
let set4: Set = [3, 4, 5, 6]

print(set1.isSuperset(of: set2))
print(set1.isStrictSuperset(of: set2))
print(set1.isSuperset(of: set3))
print(set1.isSuperset(of: set4))

The output will be as follows. isSuperset() returns true if set1 is equal to or a superset of the argument Set. Since set1 and set2 are equal, isStrictSuperset() returns false.

true
false
true
false

Checking if Sets are Disjoint with isDisjoint()

By using the isDisjoint() method, you can check if two Swift Sets are disjoint (i.e., have no elements in common).

If the two Sets share even a single element, it returns false.


Let's use isDisjoint() to check if set1 and other Sets are disjoint.

let set1: Set = [1, 2, 3, 4, 5]
let set2: Set = [6, 7, 8]
let set3: Set = [5, 6, 7]

print(set1.isDisjoint(with: set2))
print(set1.isDisjoint(with: set3))

The output will be as follows. set1 and set2 have no elements in common, so it prints true. set1 and set3 both contain 5, so it prints false.

true
false

That wraps up how to check whether two Swift Sets are equal, subsets, supersets, or disjoint.