π
Flatten Deep
Flatten Deepμ΄λ €μ λ°°μ΄ +40pts
Problem
Write a function that flattens a nested array to any depth.
Examples
Input:
flattenDeep([1, [2, [3, [4]]]])Output:
[1, 2, 3, 4]Explanation
μ΄ λ¬Έμ λ **μ¬κ·μ Array.isArray**λ₯Ό μ¬μ©νμ¬ μ€μ²© λ°°μ΄μ μμ ν ννννλ λ°©λ²μ νμ΅ν©λλ€. ## ν΅μ¬ κ°λ : κΉμ ννν λͺ¨λ μ€μ²© μμ€μ μ¬κ·μ μΌλ‘ νννν©λλ€. ### ꡬν ```javascript function flattenDeep(arr) { return arr.reduce((flat, item) => flat.concat(Array.isArray(item) ? flattenDeep(item) : item), []); } ``` ### λμ μ리 1. κ° μμκ° λ°°μ΄μΈμ§ νμΈ 2. λ°°μ΄...
View detailed explanation βKey Concepts
μ¬κ· Array.isArray κΉμ ννν reduce
Time: O(n) Space: O(n)
solution.js
Ctrl + Enter
Run tests to see results here.