๐
Count Elements
Count Elements์ฌ์ ๋ฐฐ์ด +10pts
Problem
Write a function that counts how many times a value appears in an array.
Examples
Input:
count([1, 2, 1, 3, 1], 1)Output:
3Explanation
์ด ๋ฌธ์ ๋ **filter์ length๋ฅผ ์กฐํฉ**ํ์ฌ ํน์ ๊ฐ์ ์ถํ ํ์๋ฅผ ์ธ๋ ๋ฐฉ๋ฒ์ ํ์ตํฉ๋๋ค. **filter๋ก ์นด์ดํ ํ๊ธฐ** filter๋ก ์กฐ๊ฑด์ ๋ง๋ ์์๋ง ์ถ์ถํ ํ length๋ก ๊ฐ์๋ฅผ ์ ๋๋ค. **[1, 2, 1, 3, 1]์์ 1 ์ธ๊ธฐ** 1. filter(x => x === 1) โ [1, 1, 1] 2. [1, 1, 1].length โ 3 **reduce๋ก ๊ตฌํ (๋์)** ```javascript arr.reduce((count, x) => x === value ? count + 1 : count, ...
View detailed explanation โKey Concepts
filter ๋ฉ์๋ ๋ฐฐ์ด ๊ธธ์ด ์์ ์นด์ดํ
reduce ๋์
Time: O(n) Space: O(n)
solution.js
Ctrl + Enter
Run tests to see results here.