← All Exercises

Character Frequency Map

basicjavascript~10min

Background

Frequency counting is fundamental in compression algorithms, statistical text analysis, and anagram detection.

Problem

Write a function that takes a string and returns an object where keys are characters and values are their occurrence counts.

Input

A string of zero or more characters.

Output

An object mapping each character to its count in the string.

Sample tests

Sample 1

Input: "hello"
Expected: {h:1, e:1, l:2, o:1}

Your solution

Learning objective

Learn to build frequency maps using objects and the logical OR pattern for initialization.