📦

Count Occurrences

Count Occurrences
쉬움 객체 +10pts

Problem

Write a function that counts the occurrences of each element in an array.

Examples

Input: countOccurrences(["a", "b", "a", "c", "a", "b"])
Output: { a: 3, b: 2, c: 1 }

Explanation

이 문제는 **객체를 해시맵처럼 사용**하여 빈도수를 세는 패턴을 학습합니다. 데이터 분석과 알고리즘 문제에서 자주 사용됩니다. **핵심 패턴: 객체로 카운팅** 객체의 키로 요소를, 값으로 출현 횟수를 저장합니다. **reduce로 구현하기** ```javascript arr.reduce((acc, item) => { acc[item] = (acc[item] || 0) + 1; return acc; }, {}); ``` **핵심 표현: (acc[item] || 0) + 1** - acc[item]이 존재하면: 기존 값...

View detailed explanation →

Key Concepts

객체를 해시맵으로 사용 reduce로 객체 생성 빈도수 카운팅 패턴 || 연산자 활용
Time: O(n) Space: O(k)
solution.js
Ctrl + Enter
Run tests to see results here.