0
0
Swiftprogramming~20 mins

Set creation and operations in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Set 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 sets?
Consider the following Swift code that creates and manipulates sets. What will be printed?
Swift
let setA: Set<Int> = [1, 2, 3, 4]
let setB: Set<Int> = [3, 4, 5, 6]
let unionSet = setA.union(setB)
print(unionSet.sorted())
A[3, 4]
B[1, 2]
C[1, 2, 3, 4, 5, 6]
D[5, 6]
Attempts:
2 left
💡 Hint
Think about what the union operation does to two sets.
Predict Output
intermediate
2:00remaining
What does this Swift code print after set intersection?
Look at this Swift code that finds the intersection of two sets. What is the output?
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[10, 20]
B[]
C[50, 60]
D[30, 40]
Attempts:
2 left
💡 Hint
Intersection returns only elements common to both sets.
Predict Output
advanced
2:00remaining
What is the output of this Swift code using symmetricDifference?
This Swift code uses symmetricDifference on two sets. What will be printed?
Swift
let set1: Set<Int> = [1, 2, 3, 4, 5]
let set2: Set<Int> = [4, 5, 6, 7]
let symDiff = set1.symmetricDifference(set2)
print(symDiff.sorted())
A[1, 2, 3, 6, 7]
B[4, 5]
C[1, 2, 3, 4, 5, 6, 7]
D[6, 7]
Attempts:
2 left
💡 Hint
Symmetric difference returns elements in either set but not in both.
Predict Output
advanced
2:00remaining
What is the count of elements after subtracting sets in Swift?
Given these two sets, what is the count of elements in the result after subtracting setB from setA?
Swift
let setA: Set<Int> = [100, 200, 300, 400, 500]
let setB: Set<Int> = [300, 400, 600]
let differenceSet = setA.subtracting(setB)
print(differenceSet.count)
A2
B3
C4
D5
Attempts:
2 left
💡 Hint
Subtracting removes elements of setB from setA.
🧠 Conceptual
expert
3:00remaining
Which Swift set operation produces this output?
You have two sets: setM = [2, 4, 6, 8] and setN = [1, 2, 3, 4]. Which operation on setM and setN produces the output [1, 3, 6, 8] when sorted?
AsetM.union(setN).subtracting(setM.intersection(setN))
BsetM.symmetricDifference(setN)
CsetM.intersection(setN).union(setM.subtracting(setN))
DsetM.subtracting(setN).union(setN.subtracting(setM))
Attempts:
2 left
💡 Hint
Think about how union, intersection, and subtracting combine to form symmetric difference.