Summer sale! Get 10% off annual plan with the code SUMMERSALE23, grab your discount today! Check out other promotions

Flatten

Author
Zhenghao He
Zhenghao HeSenior Engineer, Ex-Amazon
Languages
JSTS
Difficulty
Medium
Recommended duration to spend during interviews
20 mins
Users completed

Implement a function flatten that returns a newly-created array with all sub-array elements concatenated recursively into a single level.

Examples

// Single-level arrays are unaffected.
flatten([1, 2, 3]); // [1, 2, 3]
// Inner arrays are flattened into a single level.
flatten([1, [2, 3]]); // [1, 2, 3]
flatten([
[1, 2],
[3, 4],
]); // [1, 2, 3, 4]
// Flattens recursively.
flatten([1, [2, [3, [4, [5]]]]]); // [1, 2, 3, 4, 5]

Companies

Premium FeaturePurchase premium to see companies which ask this question.
View plans

Try these questions next

Similar Questions

Loading editor