steamrollArray

Medium · Array Manipulations · 2 test cases

Flatten a nested array. You must account for varying levels of nesting.

Examples

steamrollArray([[['a']], [['b']]]) → ['a', 'b']
steamrollArray([1, [2], [3, [[4]]]]) → [1, 2, 3, 4]

Starter code

function steamrollArray(arr) {

}

Complexity of the optimal solution

Time O(n), space O(n). The `flat(Infinity)` method recursively flattens the array. It visits each of the n elements in the nested structure exactly once to create the new flat array. Therefore, time and space complexity are both linear.

Solve steamrollArray in the browser