Write a C++ Program to count frequency of elements in an array| AskTheCode

Asked by: Umar Khan | Category: C++ ( c plus plus ) | CPP program to count frequency of elements in an array

#include <bits/stdc++.h>

using namespace std;

void countFreqOfElement(int arr[], int n){

vector<bool> visited(n, false);

for (int i = 0; i < n; i++) {

if (visited[i] == true)


int count = 1;

for (int j = i + 1; j < n; j++) {

if (arr[i] == arr[j]) {

visited[j] = true;




cout<< "\n" <<arr[i] <<" occured ->" << count <<" times" << endl;



int main(){

int n;

cout<< "Enter the number of elements: ";


int arr[n];

for(int i=0; i<n; i++){

cout<< "Enter "<<i+1 <<"th element: ";



cout << "\nThe array is:";

for(int i=0; i<n; i++){

cout << " "<<arr[i];


countFreqOfElement(arr, n);

return 0;


29 views0 comments

Recent Posts

See All

C Program To Add Two User Given Polynomials

C program to add two polynomials | Ask The Code C Program to find the result of addition of two given polynomials by the user...Read more...

"Believe you can and you're halfway there"