Practice Questions
Questions related to flatten array
"Flattening" an array means converting nested arrays into a single flat array containing all elements without nesting.
What does it mean to "flatten" an array?
It's the process of converting a multi-dimensional array ([1, [2, [3]]]) into a one-dimensional one ([1, 2, 3]).
2. How to implement flatten without built-in methods?
With recursion:
function flatten(arr) {
return arr.reduce(
(acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val),
[]
);
}Here reduce combines elements, recursively flattening nested arrays.
3. How to use the built-in flat() method?
[1, [2, [3, [4]]]].flat(2); // [1, 2, 3, [4]]
[1, [2, [3, [4]]]].flat(Infinity); // completely flattensThe depth parameter determines the flattening depth.
4. Can you flatten without recursion?
Yes, using a stack (iterative way):
function flattenIterative(arr) {
const stack = [...arr];
const res = [];
while (stack.length) {
const next = stack.pop();
Array.isArray(next) ? stack.push(...next) : res.push(next);
}
return res.reverse();
}5. What are typical mistakes?
- Skipping the
Infinityparameter inflat()if the depth is unknown. - Using
concatwithout considering nested levels. - Mutating the original array during iteration.