Understanding the Frequency Counter Program in C++
This is a program designed to calculate frequency for each distinct element in the array. Since we use just a simple counter mechanism, this determines how often each element may appear in an array. Please see below the code and in detail how exactly the code works:
#include<iostream>
using namespace std;
void freq(int *arr, int n){
int count[100];
for(int i = 0; i < n; i++){
count[i] = 0;
}
for(int i = 0; i < n; i++){
int idx = arr[i];
if (count[idx] == 0){
count[idx] = 1;
}else{
count[idx]++;
}
}
for(int i = 0; i < n; i++){
if(count[i] != 0){
cout<<i<<"="<<count[i]<<endl;
}
}
}
int main(){
int arr[] = {1,4,1,3,2,4,3};
int n = sizeof(arr)/sizeof(int);
freq(arr,n);
}
Code Breakdown and Explanation
Header File:

Frequency Calculation Function:
void freq(int *arr, int n) {
int count[100];
Parameters
arr: A pointer to the given input array.
n: The length of the array.
Frequency Array (count):
This array holds the count of each element.
It is initialized to size 100, assuming that array values will not exceed this range.
Initializing the Frequency Array:
for (int i = 0; i < n; i++) {
count[i] = 0;
}
Each element of the count array is set to 0 to ensure accurate counting.
Counting Element Frequencies:
for (int i = 0; i < n; i++) {
int idx = arr[i];
if (count[idx] == 0) {
count[idx] = 1;
} else {
count[idx]++;
}
}
Index Mapping:
The value of each element in arr is used as an index in the count array.
Incrementing Count:
If the value at count[idx] is 0, it is initialized to 1.
Otherwise, it is incremented by 1.
Printing the Frequencies:
for (int i = 0; i < n; i++) {
if (count[i] != 0) {
cout << i << "=" << count[i] << endl;
}
}
The program iterates over the count array to print the frequency of each element that occurs in the input array.
Main Function:
int main() {
int arr[] = {1, 4, 1, 3, 2, 4, 3};
int n = sizeof(arr) / sizeof(int);
freq(arr, n);
}
Input Array:
It contains the integers whose frequencies are to be computed.
Computing Frequencies:
It invokes the freq function with the array and its size as arguments.
Example Run
Input Array:
arr[] = {1, 4, 1, 3, 2, 4, 3}
Step-by-Step Run:
Set the count array to zeros.
For each element in arr, update its corresponding index in count.
Traverse the count array and display non-zero frequencies.
Output:
1=2
2=1
3=2
4=2
Key Concepts in the Program
Frequency Calculation:
A direct mapping between the value of an element and its index in the count array enables efficient counting.
Space Efficiency:
The program uses an auxiliary array (count) to store frequencies, with a size of 100 for simplicity.
However, this can be optimized to fit the range of elements in the input array.
Edge Cases:
The program assumes all elements are positive integers within the range of 0 to 99.
For a broader range, modifications to the count array size are necessary.
Optimization Suggestions
Dynamic Range Adjustment:
Instead of a fixed-size count array, dynamically adjust its size based on the maximum value in the input array.
Why This Program is Useful
Simplicity:
The logic is quite straightforward and, therefore easily understandable even for beginners.
Practical Application
Frequency counting is extensively used in data analysis, histograms, and pattern recognition.