Search

# 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)

continue;

int count = 1;

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

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

visited[j] = true;

count++;

}

}

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

}

}

int main(){

int n;

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

cin>>n;

int arr[n];

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

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

cin>>arr[i];

}

cout << "\nThe array is:";

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

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

}

countFreqOfElement(arr, n);

return 0;

}

31 views0 comments

### Recent Posts

See All

#### Solubility - CodeChef Solution in Java and C++| AskTheCode

CodeChef May Long Challenge Solution | Solubility (SOLBLTY) solution | AskTheCode Solubility (SOLBLTY) solution Problem: Suppose for a unit rise in temperature, the solubility of sugar in water increa

#### Valid Paths CodeChef Solution in C++ | AskTheCode

CodeChef May Long Challenge Solution | Valid Paths (VPATH) solution | AskTheCode Valid Paths (VPATH) CodeChef solution Problem: You are given a tree with N nodes numbered from 1 to N. A set S of nodes

#### Modular Equation CodeChef May Long Challenge 2021 Soution | AskTheCode

CodeChef May Long Challenge Solution | Modular Equation (MODEQ) solution | AskTheCode Modular Equation (MODEQ) solution Problem: Given integers N and M, find the number of ordered pairs (a,b) such tha