Bird
0
0

Given two sets:

hard📝 Application Q15 of 15
Swift - Collections
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?
Alet result = setX.symmetricDifference(setY)
Blet result = setX.union(setY)
Clet result = setX.intersection(setY)
Dlet result = setX.subtracting(setY)
Step-by-Step Solution
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]
Quick Trick: SymmetricDifference = items in one set only [OK]
Common Mistakes:
  • Using union instead of symmetricDifference
  • Confusing intersection with symmetricDifference
  • Using subtracting which is one-sided

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes