Consider two sets of integers. What will be printed after performing a union?
let setA: Set<Int> = [1, 2, 3] let setB: Set<Int> = [3, 4, 5] let unionSet = setA.union(setB) print(unionSet.sorted())
Union combines all unique elements from both sets.
The union of two sets contains all elements from both sets without duplicates. Here, 3 is common, so it appears once.
Find the common elements between two sets.
let setX: Set<Int> = [10, 20, 30, 40] let setY: Set<Int> = [30, 40, 50, 60] let intersectionSet = setX.intersection(setY) print(intersectionSet.sorted())
Intersection returns only elements present in both sets.
Only 30 and 40 are in both setX and setY, so the intersection is {30, 40}.
Find elements in the first set that are not in the second set.
let setM: Set<Int> = [5, 10, 15, 20] let setN: Set<Int> = [10, 20, 30] let differenceSet = setM.subtracting(setN) print(differenceSet.sorted())
Difference removes elements of the second set from the first set.
Elements 10 and 20 are removed from setM because they appear in setN. Leftover are 5 and 15.
Choose the correct set operation that returns elements exclusive to each set, excluding common elements.
Think about elements that are unique to each set, not shared.
symmetricDifference returns elements that are in one set or the other but not both, unlike union or intersection.
Analyze the combined set operations and determine the final printed array.
let a: Set<Int> = [1, 2, 3, 4] let b: Set<Int> = [3, 4, 5, 6] let c: Set<Int> = [4, 5, 6, 7] let result = a.union(b).intersection(c).subtracting([7]) print(result.sorted())
Break down the operations step-by-step: union, then intersection, then subtracting.
First, a.union(b) = {1,2,3,4,5,6}. Then intersect with c = {4,5,6,7} gives {4,5,6}. Finally, subtract 7 leaves {4,5,6}.