
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- JavaScript program for counting frequencies of array elements
- Array elements with prime frequencies in C++?
- Array elements with prime frequencies?
- Count frequencies of all elements in array in Python\n
- Counting unique elements in an array in JavaScript
- Count frequencies of all elements in array in Python using collections module
- Javascript Program for Range Queries for Frequencies of array elements
- Counting elements of an array using a recursive function in JS?
- Counting the frequencies in a list using dictionary in Python
- Counting elements in two arrays using C++
- Counting the occurrences of JavaScript array elements and put in a new 2d array
- Counting below / par elements from an array - JavaScript
- Count frequencies of all elements in array in O(1) extra space and O(n) time in C++
- Counting cross lines in an array in C++
- Counting common elements in MySQL?
