C++ program to find the frequency of elements in an array

C++ program to find the frequency of elements in an array

In this article we will learn about how to find frequency of element in an array in Cpp . Given an array, the task is to find the frequency of all the elements of the array.

For example,

Input:  arr = {1, 2, 3, 5, 2, 4, 5, 1}

Output:  1->2 times, 2->2 times, 3->1 times, 5->2 times, 4->1 times

 

C++ program to find the frequency of elements in an array

Algorithm

  1. Input the number of elements of an array.
  2. Input the array elements.
  3. Create another array to store the frequency of elements.
  4. Traverse the input array and update the count of the elements in the frequency array.
  5. Print the frequency array which displays the frequency of all the elements of the array
Cpp program to find the frequency of elements in an array

C++ Code:-

#include 
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[100],n;
   //take size of array as input in n
   cout<<"Enter size of array"; cin>>n;
   //Enter elements of the array
   cout<<"Enter element of the array";
   for(int i=0;i<n;i++){ cin>>arr[i];
   }
   //call function to calculate the frequency
   frequency(arr, n);
   return 0;
}

Output

Enter size of array 8
Enter element of the array 4
1
2
4
1
3
6
6
Frequency of 4 is: 2
Frequency of 1 is: 2
Frequency of 2 is: 1
Frequency of 3 is: 1
Frequency of 6 is: 2

Important Codes related to Arrays

  • Find Smallest Element in an Array : C | C++ | Java | Python
  • Find Second Smallest Element in an Array : C | C++ | Java | Python
  • Find Largest element in an array : C | C++ | Java | Python
  • Find the Smallest and largest element in an array : C | C++ | Java | Python
  • Calculate the sum of elements in an array : C | C++ | Java | Python
  • Reverse an Array : C | C++ | Java | Python
  • Sort first half in ascending order and second half in descending : C | C++ | Java | Python
  • Sort the elements of an array : C | C++ | Java | Python
  • Finding the frequency of elements in an array : C | C++ | Java | Python
  • Finding the Longest Palindrome in an Array : C | C++ | Java| Python
  • Counting Distinct Elements in an Array : C | C++ | Java| Python
  • Finding  Repeating elements in an Array : C | C++ | Java | Python
  • Finding Non Repeating elements in an Array : C | C++ | Java | Python
  • Removing Duplicate elements from an array : C | C++ | Java
  • Finding Minimum scalar product of two vectors : C | C++ | Java | Python
  • Finding Maximum scalar product of two vectors in an array : C | C++ | Java | Python
  • Counting the number of even and odd elements in an array : C | C++ | Java | Python
  • Find all Symmetric pairs in an array : C | C++ | Java | Python
  • Find maximum product sub-array in a given array : C | C++ | Java | Python
  • Finding Arrays are disjoint or not : C | C++ | Java | Python
  • Determine Array is a subset of another array or not : C | C++ | Java | Python
  • Determine can all numbers of an array be made equal : C | C++ | Java | Python
  • Finding Minimum sum of absolute difference of given array : C | C++ | Java | Python
  • Sorting elements of an array by frequency : C | C++ | Java | Python
  • Sort an array according to the order defined by another array : C | C++ | Java | Python
  • Replace each element of the array by its rank in the array : C | C++ | Java | Python
  • Finding equilibrium index of an array : C | C++ | Java| Python
  • Rotation of elements of array- left and right : C | C++ | Java| Python
  • Block swap algorithm for array rotation : C | C++ | Java| Python
  • Juggling algorithm for array rotation : C | C++ | Java | Python
  • Finding Circular rotation of an array by K positions : C | C++ | Java | Python

You can find the below Programs in all languages

array