0
Hours
0
Minutes
0
Seconds
You need Login/Signup to run/submit the code.

How to create a flat version of a deeply nested array? [Programming Interview Question]

@Yomesh Gupta
5555

Given a deeply nested array, create a function on the array, namely flatten, that when invoked returns a flat version of the original array. Function should be defined in a way that it can be invoked on existing and future arrays.

Demo:

var input = [
  1, 2, 3,
   [4],
  [5, 6, [7], [8, [9, [10]]]],
  11, 12, 13,
  [14, [[[[[15, [16]]]]]]],
  17, 18,
  [19, [20, [21, [22, [23, [24, [[[[[25]]]]]]]]]]]
 ];
  
var flatArray = input.flatten();
 // flatArray: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]

Loading IDE...