📚
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.