πŸ“š

Flatten Array

Flatten Array
보톡 λ°°μ—΄ +20pts

Problem

Write a function that flattens a nested array one level deep.

Examples

Input: flatten([[1, 2], [3, 4], [5]])
Output: [1, 2, 3, 4, 5]

Explanation

이 λ¬Έμ œλŠ” **λ°°μ—΄ 평탄화(flattening)**의 κ°œλ…κ³Ό **flat() λ©”μ„œλ“œ**λ₯Ό ν•™μŠ΅ν•©λ‹ˆλ‹€. **λ°°μ—΄ ν‰νƒ„ν™”λž€?** μ€‘μ²©λœ 배열을 단일 레벨의 λ°°μ—΄λ‘œ λ³€ν™˜ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€: [[1, 2], [3, 4]] β†’ [1, 2, 3, 4] **flat() λ©”μ„œλ“œ** ES2019에 μΆ”κ°€λœ flat()은 배열을 ν‰νƒ„ν™”ν•©λ‹ˆλ‹€: - `arr.flat()` - 1단계 평탄화 (κΈ°λ³Έκ°’) - `arr.flat(2)` - 2단계 평탄화 - `arr.flat(Infinity)` - μ™„μ „ 평탄화 **[[1, 2], [3, 4], [5]] 처리** ...

View detailed explanation β†’

Key Concepts

flat() λ©”μ„œλ“œ λ°°μ—΄ 평탄화 concat λ©”μ„œλ“œ reduce ν™œμš©
Time: O(n) Space: O(n)
solution.js
Ctrl + Enter
Run tests to see results here.