-
Notifications
You must be signed in to change notification settings - Fork 0
/
flattenDeeplyNested.ts
28 lines (19 loc) · 1017 Bytes
/
flattenDeeplyNested.ts
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
26
27
28
/*
Given a multi-dimensional array arr and a depth n, return a flattened version of that array.
A multi-dimensional array is a recursive data structure that contains integers or other multi-dimensional arrays.
A flattened array is a version of that array with some or all of the sub-arrays removed and replaced with the actual elements in that sub-array. This flattening operation should only be done if the current depth of nesting is less than n. The depth of the elements in the first array are considered to be 0.
Please solve it without the built-in Array.flat method.
*/
type MultiDimensionalArray = (number | MultiDimensionalArray)[];
function flat(arr: MultiDimensionalArray, n: number): MultiDimensionalArray {
if (n <= 0) return arr;
let updated: MultiDimensionalArray = [];
for (let i = 0; i < arr.length; i++) {
if (typeof arr[i] === 'number') {
updated.push(arr[i]);
} else {
updated.push(...flat(arr[i] as MultiDimensionalArray, n - 1));
}
}
return updated;
}