Bird
0
0

Why does the enumerated() method return a sequence of tuples instead of a dictionary when iterating over an array in Swift?

hard📝 Conceptual Q10 of 15
Swift - Collections
Why does the enumerated() method return a sequence of tuples instead of a dictionary when iterating over an array in Swift?
ABecause dictionaries are unordered and enumerated() needs to sort elements
BBecause tuples are faster to create than dictionaries
CBecause enumerated() modifies the original array to tuples
DBecause arrays have ordered elements and enumerated() preserves order with index-element pairs
Step-by-Step Solution
Solution:
  1. Step 1: Understand array order and enumerated() purpose

    Arrays keep elements in order. enumerated() pairs each element with its index to preserve this order.
  2. Step 2: Compare tuples vs dictionaries

    Dictionaries are unordered collections, so enumerated() returns tuples to keep order and allow iteration with indices.
  3. Final Answer:

    Because arrays have ordered elements and enumerated() preserves order with index-element pairs -> Option D
  4. Quick Check:

    enumerated() returns ordered index-element tuples [OK]
Quick Trick: enumerated() keeps order using tuples, not dictionaries [OK]
Common Mistakes:
  • Thinking enumerated() returns dictionary
  • Confusing order preservation
  • Assuming performance reason only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes