Write a function called flattenArray that takes in an array containing nested arrays of integers and returns a new array with all the integers from the nested arrays flattened into a single level.
/**
* Returns a flattened array.
* @param {number[]} arr - The array containing nested arrays.
* @returns {number[]} - The flattened array.
*/
function flattenArray(arr: number[]): number[];flattenArray([1, [2, 3], [4, 5, [6]]]); // should return [1, 2, 3, 4, 5, 6]
flattenArray([
[1, 2],
[3, [4, 5]],
[6, [7]],
]); // should return [1, 2, 3, 4, 5, 6, 7]
flattenArray([1, [2, [3, [4, [5]]]]]); // should return [1, 2, 3, 4, 5]- The input array can contain nested arrays of any depth
- The input array can contain any number of nested arrays
- You can use recursion to traverse the nested arrays and flatten them.
- If the current element is an array, you can recursively call the
flattenArrayfunction on that element to flatten it further.
Click For Solution
function flattenArray(arr) {
let result = [];
for (const item of arr) {
if (Array.isArray(item)) {
result = result.concat(flattenArray(item));
} else {
result.push(item);
}
}
return result;
}- Create a variable
resultto store the flattened array. - Loop through the input array using a
for...ofloop. - If the current element is an array, recursively call the
flattenArrayfunction on that element to flatten it further, and then concatenate the result to theresultarray. - If the current element is not an array, push it to the
resultarray. - Return the
resultarray.
The base case is implicitly handled within the loop structure. As the loop iterates through each element of the input array arr, the recursion eventually reaches the point where there are no more elements left to process.
When the input array arr is empty, the loop will not execute, and the function will directly return the empty result array.
test('Flatten Nested Arrays', () => {
expect(flattenArray([1, [2, 3], [4, 5, [6]]])).toEqual([1, 2, 3, 4, 5, 6]);
expect(
flattenArray([
[1, 2],
[3, [4, 5]],
[6, [7]],
])
).toEqual([1, 2, 3, 4, 5, 6, 7]);
expect(flattenArray([1, [2, [3, [4, [5]]]]])).toEqual([1, 2, 3, 4, 5]);
});