Bird
Raised Fist0
Swiftprogramming~20 mins

Set algebra (union, intersection, difference) in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Set Algebra Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Swift code using set union?

Consider two sets of integers. What will be printed after performing a union?

Swift
let setA: Set<Int> = [1, 2, 3]
let setB: Set<Int> = [3, 4, 5]
let unionSet = setA.union(setB)
print(unionSet.sorted())
A[1, 2, 3, 4, 5]
B[1, 2, 4, 5]
C[3]
D[1, 2, 3]
Attempts:
2 left
💡 Hint

Union combines all unique elements from both sets.

❓ Predict Output
intermediate
2:00remaining
What is the output of this Swift code using set intersection?

Find the common elements between two sets.

Swift
let setX: Set<Int> = [10, 20, 30, 40]
let setY: Set<Int> = [30, 40, 50, 60]
let intersectionSet = setX.intersection(setY)
print(intersectionSet.sorted())
A[30, 40]
B[10, 20, 30, 40, 50, 60]
C[50, 60]
D[10, 20]
Attempts:
2 left
💡 Hint

Intersection returns only elements present in both sets.

❓ Predict Output
advanced
2:00remaining
What is the output of this Swift code using set difference?

Find elements in the first set that are not in the second set.

Swift
let setM: Set<Int> = [5, 10, 15, 20]
let setN: Set<Int> = [10, 20, 30]
let differenceSet = setM.subtracting(setN)
print(differenceSet.sorted())
A[30]
B[10, 20]
C[5, 15]
D[5, 10, 15, 20, 30]
Attempts:
2 left
💡 Hint

Difference removes elements of the second set from the first set.

🧠 Conceptual
advanced
1:30remaining
Which Swift set operation returns elements that are in either set but not in both?

Choose the correct set operation that returns elements exclusive to each set, excluding common elements.

Aintersection(_:)
BsymmetricDifference(_:)
Cunion(_:)
Dsubtracting(_:)
Attempts:
2 left
💡 Hint

Think about elements that are unique to each set, not shared.

❓ Predict Output
expert
2:30remaining
What is the output of this Swift code combining multiple set operations?

Analyze the combined set operations and determine the final printed array.

Swift
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())
A[1, 2, 3]
B[3, 4]
C[5, 6, 7]
D[4, 5, 6]
Attempts:
2 left
💡 Hint

Break down the operations step-by-step: union, then intersection, then subtracting.

Practice

(1/5)
1. What does the union operation do when applied to two Swift sets?
easy
A. Combines all unique elements from both sets into one set.
B. Finds elements that are only in the first set.
C. Finds elements common to both sets.
D. Removes all elements from both sets.

Solution

  1. Step 1: Understand the union operation

    The union of two sets combines all unique elements from both sets without duplicates.
  2. 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.
  3. Final Answer:

    Combines all unique elements from both sets into one set. -> Option A
  4. Quick Check:

    union = all unique elements combined [OK]
Hint: Union means all unique items from both sets combined [OK]
Common Mistakes:
  • Confusing union with intersection
  • Thinking union removes elements
  • Mixing union with difference
2. Which of the following is the correct Swift syntax to find the intersection of two sets a and b?
easy
A. let result = a + b
B. let result = a.intersection(b)
C. let result = a.union(b)
D. let result = a - b

Solution

  1. Step 1: Recall Swift set intersection syntax

    In Swift, the method to find common elements is intersection(), called as a.intersection(b).
  2. Step 2: Check other options

    a + b is not valid syntax, a.union(b) finds all unique elements, and a - b finds difference, so only a.intersection(b) is correct.
  3. Final Answer:

    let result = a.intersection(b) -> Option B
  4. Quick Check:

    intersection uses .intersection() method [OK]
Hint: Use .intersection() method to find common elements [OK]
Common Mistakes:
  • Using + instead of .intersection()
  • Confusing union and intersection methods
  • Using subtraction for intersection
3. What is the output of the following Swift code?
let setA: Set<Int> = [1, 2, 3, 4]
let setB: Set<Int> = [3, 4, 5, 6]
let result = setA.subtracting(setB)
print(result.sorted())
medium
A. [5, 6]
B. [3, 4]
C. [1, 2]
D. [1, 2, 3, 4, 5, 6]

Solution

  1. Step 1: Understand subtracting operation

    setA.subtracting(setB) returns elements in setA that are NOT in setB. Here, setA is [1,2,3,4], setB is [3,4,5,6].
  2. Step 2: Calculate difference

    Elements 3 and 4 are in both sets, so they are removed. Remaining elements in setA are 1 and 2.
  3. Final Answer:

    [1, 2] -> Option C
  4. Quick Check:

    subtracting = elements only in first set [OK]
Hint: Subtracting removes elements found in second set from first [OK]
Common Mistakes:
  • Confusing subtracting with intersection
  • Expecting union instead of difference
  • Not sorting output before printing
4. The following Swift code is intended to find the union of two sets 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)
medium
A. union cannot be used with string sets.
B. Sets must be arrays to use union.
C. The print statement is incorrect.
D. Missing parentheses after union method call.

Solution

  1. Step 1: Check method call syntax

    In Swift, methods require parentheses even if they take parameters. The code uses setX.union setY without parentheses.
  2. Step 2: Correct syntax for union

    The correct call is setX.union(setY). Other options are incorrect because union works on sets of strings and print is valid.
  3. Final Answer:

    Missing parentheses after union method call. -> Option D
  4. Quick Check:

    Method calls need parentheses [OK]
Hint: Always use parentheses when calling methods [OK]
Common Mistakes:
  • Omitting parentheses on method calls
  • Thinking union only works on numbers
  • Misunderstanding print syntax
5. Given two sets of integers:
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?
hard
A. All of the above
B. let result = set1.symmetricDifference(set2)
C. let result = set1.subtracting(set2).union(set2.subtracting(set1))
D. let result = set1.union(set2).subtracting(set1.intersection(set2))

Solution

  1. Step 1: Understand symmetric difference

    Symmetric difference means elements in either set but not in both.
  2. Step 2: Analyze each option

    let result = set1.symmetricDifference(set2) uses built-in symmetricDifference() 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.
  3. Step 3: Confirm all options produce same result

    All three approaches correctly compute symmetric difference.
  4. Final Answer:

    All of the above -> Option A
  5. Quick Check:

    Multiple ways to get symmetric difference [OK]
Hint: Symmetric difference = union minus intersection or use built-in method [OK]
Common Mistakes:
  • Using only union or intersection alone
  • Confusing difference with symmetric difference
  • Not combining differences both ways
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard