Bird
0
0

You want to find elements that are in setA but not in setB, and then add elements from setC that are also in setB. Which Swift code correctly does this?

hard📝 Application Q9 of 15
Swift - Collections
You want to find elements that are in setA but not in setB, and then add elements from setC that are also in setB. Which Swift code correctly does this?
let setA: Set = [1, 2, 3, 4]
let setB: Set = [3, 4, 5, 6]
let setC: Set = [4, 5, 6, 7]
Alet result = setA.subtracting(setB).union(setC.intersection(setB))
Blet result = setA.intersection(setB).union(setC.subtracting(setB))
Clet result = setA.union(setB).subtracting(setC)
Dlet result = setA.symmetricDifference(setB).union(setC)
Step-by-Step Solution
Solution:
  1. Step 1: Find elements in setA but not in setB

    Use setA.subtracting(setB) to get elements only in setA.
  2. Step 2: Find elements in setC that are also in setB

    Use setC.intersection(setB) to get common elements.
  3. Step 3: Combine both results

    Use union() to combine these two sets.
  4. Final Answer:

    let result = setA.subtracting(setB).union(setC.intersection(setB)) -> Option A
  5. Quick Check:

    Subtract then union with intersection [OK]
Quick Trick: Combine subtracting() and intersection() with union() [OK]
Common Mistakes:
  • Mixing intersection and subtracting order
  • Using union before subtracting
  • Confusing which sets to intersect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes