Frequency Array
How frequency array works and created
Introduction
In many problems, we need to count how many times each value appears in an array or a string. A frequency array is an auxiliary array freq[] where:
- the index represents a value (or an encoded value, such as a character);
- the element at that index stores how many times that value occurs.
For example, if the input array is , then:
A frequency array is especially useful when the range of possible values is small and known in advance.
Example 1: Counting Frequencies of Numbers
Given integers and , and an array of integers where , print how many times each number appears.