Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

flatMap to Duplicate

Working With Arraysmedium

Course · Section 11: Working With Arrays · Lecture 170: flat and flatMap

Implement `duplicateEach(arr)` returning each element twice in place, using `flatMap`.

Sample tests

Input: duplicateEach([1,2])
Output: [1,1,2,2]
Input: duplicateEach([])
Output: []

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • map alone would leave nested pairs.
Approach & explanation (try first)

flatMap maps each element to a pair and flattens the result one level.

Loading...
⌘/Ctrl + Enter

Run your code to see results.