Flatten Nested Array
JavaScript
easy
10 mins
Create a function that takes a nested array of arbitrary depth and returns a flattened array where all nested elements are extracted and placed in a single array.
Example Inputs & Outputs
//Example 1 Input: [1, [2, [3, 4], 5], 6] Output: [1, 2, 3, 4, 5, 6] //Example 2 Input: [["a"], ["b", ["c", "d"]], "e"] Output: ["a", "b", "c", "d", "e"]
Constraints & Edge Cases
- The input array may contain numbers, strings, or other data types.
- The function should handle deeply nested arrays efficiently.
- Empty arrays should be handled correctly.
- The function should return a new array without modifying the original input.
Companies:
microsoft
ola
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
