Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

Count Frequencies

Given an array of integers, count how many times each number appears. Return a hash map where keys are the numbers from the array and values are their frequencies.

Input

  • arr: an array of integers

Output

A hash map mapping each number to its frequency (count of occurrences)

Examples

Example 1:

Input: arr = [1, 2, 2, 3, 3, 3]

Output: {1: 1, 2: 2, 3: 3}

Explanation: 1 appears once, 2 appears twice, 3 appears three times.

Example 2:

Input: arr = [5, 5, 5, 5]

Output: {5: 4}

Explanation: 5 appears four times.

Example 3:

Input: arr = [1, 2, 3, 4]

Output: {1: 1, 2: 1, 3: 1, 4: 1}

Explanation: Each number appears exactly once.

Try It Yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro