
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Counting Frequencies of Array Elements in C++
We are given an array of integer elements which contains duplicate values and the task is to calculate the frequencies of the distinct elements present in an array and print the result.
Input − int arr[] = {1, 1, 2, 3, 4, 1, 2, 3}
Output −
frequency of 1 is: 3 frequency of 2 is: 2 frequency of 3 is: 2 Frequency of 4 is: 1
Input − int arr[] = {2, 3, 4, 1, 5}
Output −
frequency of 1 is: 1 frequency of 2 is: 1 frequency of 3 is: 1 Frequency of 4 is: 1 Frequency of 5 is: 1
Approach used in the below program is as follows
There can be multiple solutions for this and those can be simpler in coding terms or simpler in complexity terms. So let’s first look at the simpler approach in coding terms
Create an array of integer type variables
Calculate the size of an array using size() function.
Create a boolean array let’s say, check of array size
Start loop FOR from i to 0 and i less than size
Inside the loop, set check[i] = 0
Start loop FOR from i to 0 and i less than size
Inside the loop, check IF check[i] = 1 then continue
Declare variable count and initialise it with 1 that will print the frequency count
Start loop FOR j from i+1 till the size
Inside the loop, check if arr[i] = arr[j] then set check[j] to 1 and increment the count by 1
Print the value of count.
Another solution of it can be −
Create an array of integer type variables
Calculate the size of an array using size() function.
Create a variable of type unordered_map let’s say um
Start loop FOR from i to 0 and till size
Inside the loop, set um[arr[i]]++
Start another loop for from auto x till um
Inside the loop, print the frequency.
Example
#include <bits/stdc++.h> using namespace std; int frequency(int arr[], int size){ bool check[size]; for(int i=0;i<size;i++){ check[i] = 0; } for(int i=0; i<size; i++){ if(check[i]== 1){ continue; } int count = 1; for(int j = i+1; j<size; j++){ if (arr[i] == arr[j]){ check[j] = 1; count++; } } cout<<"frequency of "<<arr[i]<<" is: " << count << endl; } } int main(){ int arr[] = {1, 2, 3, 1, 2, 3}; //calculate the size of an array int size = sizeof(arr) / sizeof(arr[0]); //call function to calculate the frequency frequency(arr, size); return 0; }
Output
If we run the above code it will generate the following output −
frequency of 1 is: 2 frequency of 2 is: 2 frequency of 3 is: 2
Example
#include <bits/stdc++.h> using namespace std; void frequency(int arr[], int size){ unordered_map<int, int< um; for (int i = 0; i < size; i++){ um[arr[i]]++; } for (auto x : um){ cout<<"frequency of "<<x.first<<" is: "<< x.second<< endl; } } int main(){ int arr[] = {1, 2, 3, 1, 2, 3 }; int size = sizeof(arr) / sizeof(arr[0]); frequency(arr, size); return 0; }
Output
If we run the above code it will generate the following output −
frequency of 3 is: 2 frequency of 1 is: 2 frequency of 2 is: 2