Programming Examples
Cpp program to find the frequency of elements in an array
Write a cpp program to find the frequency of elements in an array
Solution
#include<iostream>
using namespace std;
int main(){
int num[5],a,b,flag;
cout<<"Enter 5 elements in an array";
for(a=0;a<5;a++)
{
cin>>num[a];
}
for(a=0;a<5;a++)
{
flag=0;
for(b=a-1;b>=0;b--)
{
if(num[a]==num[b])
{
flag=1;
break;
}
}
if(flag==0)
{
int count=0;
for( b=0;b<5;b++)
{
if(num[a]==num[b])
{
count++;
}
}
cout<<num[a]<<" -> "<<count<<endl;
}
}
return 0;
}
Output
Enter 5 elements in an array
1
1
2
2
3
1 -> 2
2 -> 2
3 -> 1