EasyPythonJavaScriptTypeScript

Flatten Array

ArraysFunctions

Implement solve(arr) that takes a nested array of arbitrary depth and
returns a brand-new, fully flattened, single-level array.

Examples

  • solve([1, [2, [3, [4]]]])[1, 2, 3, 4]
  • solve([])[]
  • solve([[1, 2], [3, 4]])[1, 2, 3, 4]

Constraints

  • The input array may contain numbers, strings or other arrays.
  • Don't mutate the input.
  • Aim for an O(n) solution where n is the total element count.

Sample tests

Test #1Already flat
Input: [[1,2,3]]
Output: [1,2,3]
Test #2Deeply nested array of numbers
Input: [[1,[2,[3,[4]]]]]
Output: [1,2,3,4]
Test #3Single deeply-nested element
Input: [[[[[[42]]]]]]
Output: [42]
Test #4Mixed strings, varying depths
Input: [["a",["b",["c"],"d"],"e"]]
Output: ["a","b","c","d","e"]
Test #5Empty array
Input: [[]]
Output: []