Set algebra helps you combine or compare groups of unique items easily. It lets you find all items together, only common items, or items that are different.
Set algebra (union, intersection, difference) in Swift
let unionSet = setA.union(setB) let intersectionSet = setA.intersection(setB) let differenceSet = setA.subtracting(setB)
Sets in Swift hold unique values with no order.
union() adds all unique elements from both sets.
let setA: Set<Int> = [1, 2, 3] let setB: Set<Int> = [3, 4, 5] let unionSet = setA.union(setB) print(unionSet)
let setA: Set<Int> = [1, 2, 3] let setB: Set<Int> = [3, 4, 5] let intersectionSet = setA.intersection(setB) print(intersectionSet)
let setA: Set<Int> = [1, 2, 3] let setB: Set<Int> = [3, 4, 5] let differenceSet = setA.subtracting(setB) print(differenceSet)
This program shows how to get all unique fruits from both sets (union), fruits common to both (intersection), and fruits only in the first set (difference). We sort the sets before printing to see the items in order.
import Foundation let setA: Set<String> = ["apple", "banana", "cherry"] let setB: Set<String> = ["banana", "dragonfruit", "apple"] let unionSet = setA.union(setB) let intersectionSet = setA.intersection(setB) let differenceSet = setA.subtracting(setB) print("Union:", unionSet.sorted()) print("Intersection:", intersectionSet.sorted()) print("Difference:", differenceSet.sorted())
Sets do not keep order, so use sorted() to print in order.
union() combines all unique elements from both sets.
intersection() finds only elements present in both sets.
subtracting() finds elements in the first set not in the second.
Use union to combine all unique items from two sets.
Use intersection to find common items between sets.
Use difference (subtracting) to find items in one set but not the other.
Practice
union operation do when applied to two Swift sets?Solution
Step 1: Understand the union operation
The union of two sets combines all unique elements from both sets without duplicates.Step 2: Compare with other set operations
Intersection finds common elements, difference finds unique elements in one set, so union is the only one combining all unique elements.Final Answer:
Combines all unique elements from both sets into one set. -> Option AQuick Check:
union = all unique elements combined [OK]
- Confusing union with intersection
- Thinking union removes elements
- Mixing union with difference
a and b?Solution
Step 1: Recall Swift set intersection syntax
In Swift, the method to find common elements isintersection(), called asa.intersection(b).Step 2: Check other options
a + bis not valid syntax,a.union(b)finds all unique elements, anda - bfinds difference, so onlya.intersection(b)is correct.Final Answer:
let result = a.intersection(b) -> Option BQuick Check:
intersection uses .intersection() method [OK]
- Using + instead of .intersection()
- Confusing union and intersection methods
- Using subtraction for intersection
let setA: Set<Int> = [1, 2, 3, 4] let setB: Set<Int> = [3, 4, 5, 6] let result = setA.subtracting(setB) print(result.sorted())
Solution
Step 1: Understand subtracting operation
setA.subtracting(setB)returns elements insetAthat are NOT insetB. Here, setA is [1,2,3,4], setB is [3,4,5,6].Step 2: Calculate difference
Elements 3 and 4 are in both sets, so they are removed. Remaining elements in setA are 1 and 2.Final Answer:
[1, 2] -> Option CQuick Check:
subtracting = elements only in first set [OK]
- Confusing subtracting with intersection
- Expecting union instead of difference
- Not sorting output before printing
setX and setY. What is the error?let setX: Set<String> = ["apple", "banana"] let setY: Set<String> = ["banana", "cherry"] let combined = setX.union setY print(combined)
Solution
Step 1: Check method call syntax
In Swift, methods require parentheses even if they take parameters. The code usessetX.union setYwithout parentheses.Step 2: Correct syntax for union
The correct call issetX.union(setY). Other options are incorrect because union works on sets of strings and print is valid.Final Answer:
Missing parentheses after union method call. -> Option DQuick Check:
Method calls need parentheses [OK]
- Omitting parentheses on method calls
- Thinking union only works on numbers
- Misunderstanding print syntax
let set1: Set<Int> = [2, 4, 6, 8, 10] let set2: Set<Int> = [1, 2, 3, 4, 5]Write Swift code to find elements that are in either set but NOT in both (symmetric difference). What is the correct way to do this?
Solution
Step 1: Understand symmetric difference
Symmetric difference means elements in either set but not in both.Step 2: Analyze each option
let result = set1.symmetricDifference(set2) uses built-insymmetricDifference()method. let result = set1.union(set2).subtracting(set1.intersection(set2)) finds union then removes intersection, which equals symmetric difference. let result = set1.subtracting(set2).union(set2.subtracting(set1)) finds difference both ways then unions them, also symmetric difference.Step 3: Confirm all options produce same result
All three approaches correctly compute symmetric difference.Final Answer:
All of the above -> Option AQuick Check:
Multiple ways to get symmetric difference [OK]
- Using only union or intersection alone
- Confusing difference with symmetric difference
- Not combining differences both ways
Ready to prove it?
Pass the test and this pattern moves unknown
