Bird
0
0

Given two arrays a = [1, 2, 3, 4] and b = [3, 4, 5, 6], how can you find elements that are in either array but not in both?

hard📝 Application Q9 of 15
Ruby - Arrays

Given two arrays a = [1, 2, 3, 4] and b = [3, 4, 5, 6], how can you find elements that are in either array but not in both?

Aa & b
Ba + b
Ca - b
D(a | b) - (a & b)
Step-by-Step Solution
Solution:
  1. Step 1: Understand symmetric difference

    Elements in either array but not both is called symmetric difference.
  2. Step 2: Use union minus intersection

    Calculate union (a | b) and subtract intersection (a & b) to get symmetric difference.
  3. Final Answer:

    Use (a | b) - (a & b) for symmetric difference -> Option D
  4. Quick Check:

    Symmetric difference = union - intersection [OK]
Quick Trick: Symmetric difference = (a | b) - (a & b) [OK]
Common Mistakes:
  • Using only & or |
  • Using + instead of |
  • Confusing difference with symmetric difference

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes