πŸ’Ž

ARR PS - find the frequency of a number

notion image
Β 
notion image
Β 

풀어보기

λ²”μœ„ λ‚΄μ˜ 숫자λ₯Ό ν•˜λ‚˜μ”© 뽑아 λ¬Έμžμ—΄ν™”ν•΄μ„œ split("") λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜λ©΄ λ°°μ—΄λ‘œ λ§Œλ“€ 수 μžˆλ‹€.
for (let i = s; i <= e; i++) {
        i.toString()
            .split("")
λ°°μ—΄μ˜ forEachλ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•΄μ„œ νƒμƒ‰ν•˜λ©΄μ„œ κ°–κ³  μžˆλŠ” μˆ«μžλ“€μ„ arr에 λˆ„μ ν•œ 것이 닡이 λœλ‹€.
Β 
function answer(s, e) {
    let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    for (let i = s; i <= e; i++) {
        i.toString()
            .split("")
            .forEach((v) => arr[v]++);
    }

    return arr;
}