Bird
Raised Fist0
Swiftprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is true about a Set in Swift?
easy
A. It can only store numbers, not strings.
B. It stores duplicate values in the order they were added.
C. It stores unique values without any specific order.
D. It always keeps elements sorted alphabetically.

Solution

  1. Step 1: Understand Set properties

    A Set in Swift stores unique values and does not keep any order.
  2. Step 2: Compare options with Set behavior

    It stores unique values without any specific order. matches the unique and unordered nature of Sets. Others describe arrays or sorted collections.
  3. Final Answer:

    It stores unique values without any specific order. -> Option C
  4. Quick Check:

    Set = unique + unordered [OK]
Hint: Remember: Sets never keep duplicates or order [OK]
Common Mistakes:
  • Thinking Sets keep order like arrays
  • Assuming Sets allow duplicates
  • Believing Sets only store numbers
2. Which of the following is the correct way to create a Set of integers in Swift?
easy
A. let numbers = {1, 2, 3, 4} as Set<Int>
B. let numbers: Set<Int> = [1, 2, 3, 4]
C. let numbers = Set(1, 2, 3, 4)
D. let numbers = [1, 2, 3, 4] as Set

Solution

  1. Step 1: Recall Set creation syntax

    In Swift, a Set can be created by specifying type and using square brackets with values.
  2. Step 2: Check each option's syntax

    let numbers: Set<Int> = [1, 2, 3, 4] uses correct syntax: Set<Int> = [1, 2, 3, 4]. Others have syntax errors or wrong casting.
  3. Final Answer:

    let numbers: Set<Int> = [1, 2, 3, 4] -> Option B
  4. Quick Check:

    Set creation = type + brackets [OK]
Hint: Use Set<Type> with brackets for values [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Trying to cast arrays directly to Set without syntax
  • Missing type declaration when needed
3. What will be the output of this Swift code?
let setA: Set<Int> = [1, 2, 3, 4]
let setB: Set<Int> = [3, 4, 5, 6]
let result = setA.intersection(setB)
print(result.sorted())
medium
A. [3, 4]
B. [1, 2]
C. [5, 6]
D. [1, 2, 3, 4, 5, 6]

Solution

  1. Step 1: Understand intersection operation

    Intersection returns elements common to both sets. Here, common elements are 3 and 4.
  2. Step 2: Check printed output

    Result is converted to a sorted array, so output is [3, 4].
  3. Final Answer:

    [3, 4] -> Option A
  4. Quick Check:

    Intersection = common elements [OK]
Hint: Intersection = only items in both sets [OK]
Common Mistakes:
  • Confusing intersection with union
  • Forgetting to sort before printing
  • Expecting all elements combined
4. Identify the error in this Swift code snippet:
var fruits: Set<String> = ["apple", "banana", "orange"]
fruits.append("grape")
medium
A. The brackets [] cannot be used to create a Set.
B. You cannot add strings to a Set of strings.
C. The Set must be declared as a constant, not variable.
D. Sets do not have an append method.

Solution

  1. Step 1: Check Set modification methods

    Sets use insert() to add elements, not append().
  2. Step 2: Analyze the code error

    Calling append() on a Set causes a compile error because it is not defined for Set.
  3. Final Answer:

    Sets do not have an append method. -> Option D
  4. Quick Check:

    Use insert() to add to Set [OK]
Hint: Use insert(), not append(), to add Set items [OK]
Common Mistakes:
  • Using append() instead of insert()
  • Thinking Sets are immutable by default
  • Confusing array and Set methods
5. Given two sets:
let setX: Set<Int> = [1, 2, 3, 4, 5]
let setY: Set<Int> = [4, 5, 6, 7]

Which Swift code correctly creates a new set containing elements in either setX or setY but not in both?
hard
A. let result = setX.symmetricDifference(setY)
B. let result = setX.union(setY)
C. let result = setX.intersection(setY)
D. let result = setX.subtracting(setY)

Solution

  1. Step 1: Understand symmetricDifference

    Symmetric difference returns elements in either set but not in both.
  2. Step 2: Compare with other operations

    Union combines all elements, intersection finds common, subtracting removes elements of second from first.
  3. Final Answer:

    let result = setX.symmetricDifference(setY) -> Option A
  4. Quick Check:

    Symmetric difference = exclusive elements [OK]
Hint: SymmetricDifference = items in one set only [OK]
Common Mistakes:
  • Using union instead of symmetricDifference
  • Confusing intersection with symmetricDifference
  • Using subtracting which is one-sided
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