Bird
0
0

Why does "a,b,a".split(',').join('-') produce "a-b-a" and not remove duplicate elements?

hard📝 Conceptual Q10 of 15
Ruby - String Operations
Why does "a,b,a".split(',').join('-') produce "a-b-a" and not remove duplicate elements?
Asplit and join do not remove duplicates; they only split and combine strings
Bsplit automatically removes duplicates but join adds them back
Cjoin removes duplicates but split adds them
DRuby strings cannot have duplicates removed
Step-by-Step Solution
Solution:
  1. Step 1: Understand split behavior

    split divides the string into parts but keeps all elements including duplicates.
  2. Step 2: Understand join behavior

    join combines array elements in order without removing duplicates.
  3. Final Answer:

    split and join do not remove duplicates; they only split and combine strings -> Option A
  4. Quick Check:

    split/join preserve duplicates, no removal [OK]
Quick Trick: Split and join do not filter duplicates [OK]
Common Mistakes:
  • Expecting split to remove duplicates
  • Thinking join filters elements
  • Confusing with set operations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes