Swift Sets: Operations on Sets

A set is an unordered collection of unique elements. In Swift, a set is defined as a generic collection type, Set<Element>, where Element can be any hashable type. Sets are used to perform various operations on unique values such as removing duplicates from an array, membership testing, and finding common values between two collections.

Creating a Set

We can create a set in Swift by using the Set keyword and providing the elements inside the brackets.

1// Creating a set of integers 2var mySet = Set([1, 2, 3, 4, 5]) 3print(mySet) // Output: [5, 2, 3, 1, 4] 4 5// Creating a set of strings 6var myStringSet: Set<String> = ["Apple", "Banana", "Cherry", "Apple"] 7print(myStringSet) // Output: ["Apple", "Banana", "Cherry"]

In the above example, we have created a set of integers and strings. Notice that the duplicate "Apple" element is automatically removed from the string set.

Adding and Removing Elements

We can add or remove elements in a set by using the insert and remove methods.

1// Creating a set of integers 2var mySet = Set([1, 2, 3, 4, 5]) 3 4// Adding a new element 5mySet.insert(6) 6print(mySet) // Output: [5, 2, 3, 1, 6, 4] 7 8// Removing an element 9mySet.remove(3) 10print(mySet) // Output: [5, 2, 1, 6, 4]

In the above example, we have added a new element 6 and removed the element 3 from the set.

Set Operations

Swift provides various operations that we can perform on sets. Some of the most commonly used set operations are:

Intersection

Intersection returns a new set that contains the common elements of two sets.

1let setA: Set = [1, 2, 3, 4, 5] 2let setB: Set = [3, 4, 5, 6, 7] 3 4let intersection = setA.intersection(setB) 5print(intersection) // Output: [5, 3, 4]

In the above example, we have created two sets and performed the intersection operation, which returns a new set that contains the common elements.

Union

Union returns a new set that contains all the unique elements from two sets.

1let setA: Set = [1, 2, 3, 4, 5] 2let setB: Set = [3, 4, 5, 6, 7] 3 4let union = setA.union(setB) 5print(union) // Output: [7, 1, 2, 5, 6, 3, 4]

In the above example, we have created two sets and performed the union operation, which returns a new set that contains all the unique elements.

Difference

Difference returns a new set that contains the elements that are in the first set but not in the second set.

1let setA: Set = [1, 2, 3, 4, 5] 2let setB: Set = [3, 4, 5, 6, 7] 3 4let difference = setA.subtracting(setB) 5print(difference) // Output: [2, 1]

In the above example, we have created two sets and performed the difference operation, which returns a new set that contains the difference elements from two sets.

Symmetric Difference

The symmetric difference of two sets is a set containing only the elements that are in either of the sets, but not in both.

1let set7: Set<Int> = [1, 2, 3] 2let set8: Set<Int> = [3, 4, 5] 3let symmetricDifferenceSet = set7.symmetricDifference(set8) 4print(symmetricDifferenceSet)

Output: [5, 1, 2, 4]

Subset and Superset

A "subset" is a set that contains only elements that are also found in another set. In other words, if set A is a subset of set B, then all elements in set A are also present in set B.

On the other hand, a "superset" is a set that contains all the elements of another set. If set B is a superset of set A, then all elements in set A are also present in set B, but set B may also contain additional elements not found in set A.

In Swift, you can determine whether a set is a subset or superset of another set using the isSubset(of:) and isSuperset(of:) methods, respectively.

Here's an example:

1let setA: Set<Int> = [1, 2, 3, 4, 5] 2let setB: Set<Int> = [1, 2, 3, 4, 5, 6, 7] 3 4if setA.isSubset(of: setB) { 5 print("setA is a subset of setB") 6} 7 8if setB.isSuperset(of: setA) { 9 print("setB is a superset of setA") 10}

Output:

1setA is a subset of setB 2setB is a superset of setA

In the above example, setA is a subset of setB because all elements in setA (1, 2, 3, 4, 5) are also present in setB. Conversely, setB is a superset of setA because it contains all elements in setA, plus some additional elements not found in setA.