Swift Tutorial
Swift Data Types
Swift Control Flow
Swift Functions
Swift Collections
Swift OOPs
Swift Additional Topics
In Swift, a Set
is an unordered collection of unique values of the same type. Because of the uniqueness property, sets in Swift are particularly useful for tasks that involve checking membership and eliminating duplicate values.
Swift provides a suite of methods and properties to perform different operations on sets. Here's an overview of the most commonly used set operations:
Insertion:
var fruits: Set = ["Apple", "Orange"] fruits.insert("Banana")
Removal:
fruits.remove("Apple")
Checking for an Element:
if fruits.contains("Orange") { print("Contains Orange!") }
Intersection (common elements between two sets):
let setA: Set = [1, 2, 3, 4] let setB: Set = [3, 4, 5, 6] let common = setA.intersection(setB) // {3, 4}
Union (all elements from both sets, removing duplicates):
let unionSet = setA.union(setB) // {1, 2, 3, 4, 5, 6}
Subtraction (elements not in the specified set):
let subtracted = setA.subtracting(setB) // {1, 2}
Symmetric Difference (elements that are either in one set or the other, but not in both):
let difference = setA.symmetricDifference(setB) // {1, 2, 5, 6}
isSubset(of:):
let setA: Set = [1, 2] let setB: Set = [1, 2, 3, 4] print(setA.isSubset(of: setB)) // true
isSuperset(of:):
print(setB.isSuperset(of: setA)) // true
isStrictSubset(of:) and isStrictSuperset(of:): These methods determine whether one set is a subset or superset of another, but not equal to it.
isDisjoint(with:) (checks if two sets have no values in common):
let setC: Set = [5, 6, 7] print(setA.isDisjoint(with: setC)) // true
These operations are beneficial when working with groups of items where membership uniqueness and relationships between groups are important. The performance of set operations in Swift is generally very efficient, especially when checking for membership or the presence of duplicates, due to the underlying implementation using hashing.
How to perform union of sets in Swift:
Description: The union of two sets is a new set containing all unique elements from both sets.
let set1: Set<Int> = [1, 2, 3, 4] let set2: Set<Int> = [3, 4, 5, 6] let unionSet = set1.union(set2) print(unionSet) // Output: [1, 2, 3, 4, 5, 6]
Intersection of sets in Swift programming:
Description: The intersection of two sets is a new set containing elements common to both sets.
let set1: Set<Int> = [1, 2, 3, 4] let set2: Set<Int> = [3, 4, 5, 6] let intersectionSet = set1.intersection(set2) print(intersectionSet) // Output: [3, 4]
Set difference and subtraction in Swift:
Description: Set difference returns a new set with elements from the first set that are not in the second set.
let set1: Set<Int> = [1, 2, 3, 4] let set2: Set<Int> = [3, 4, 5, 6] let differenceSet = set1.subtracting(set2) print(differenceSet) // Output: [1, 2]
Symmetric difference of sets in Swift:
Description: The symmetric difference of two sets is a new set with elements that are only in one of the sets, excluding common elements.
let set1: Set<Int> = [1, 2, 3, 4] let set2: Set<Int> = [3, 4, 5, 6] let symmetricDifferenceSet = set1.symmetricDifference(set2) print(symmetricDifferenceSet) // Output: [1, 2, 5, 6]
Combining sets with Swift set operations:
Description: You can combine multiple set operations to achieve specific results.
let set1: Set<Int> = [1, 2, 3, 4] let set2: Set<Int> = [3, 4, 5, 6] let set3: Set<Int> = [5, 6, 7, 8] let result = set1.intersection(set2).union(set3) print(result) // Output: [3, 4, 5, 6, 7, 8]
Filtering and mapping sets in Swift:
Description: You can use filter
and map
on sets to create new sets with filtered or transformed elements.
let numbers: Set<Int> = [1, 2, 3, 4, 5] let evenNumbers = numbers.filter { $0 % 2 == 0 } let squaredNumbers = numbers.map { $0 * $0 } print(evenNumbers) // Output: [2, 4] print(squaredNumbers) // Output: [1, 4, 9, 16, 25]
Swift subset and superset operations:
Description: You can check if a set is a subset or superset of another set.
let set1: Set<Int> = [1, 2, 3] let set2: Set<Int> = [1, 2] let isSubset = set2.isSubset(of: set1) let isSuperset = set1.isSuperset(of: set2) print(isSubset) // Output: true print(isSuperset) // Output: true
Testing set equality in Swift:
Description: Sets can be compared for equality.
let set1: Set<Int> = [1, 2, 3] let set2: Set<Int> = [3, 2, 1] let areEqual = set1 == set2 print(areEqual) // Output: true
Set operations with optionals in Swift:
Description: Sets can be used with optional types.
var optionalSet: Set<Int>? = [1, 2, 3] // Unwrapping and performing set operations if let unwrappedSet = optionalSet { let squaredNumbers = unwrappedSet.map { $0 * $0 } print(squaredNumbers) // Output: [1, 4, 9] }