AD1

Saturday, 16 September 2017

Count frequency of each element in array

Write a program to count the frequency of each element in array , as example if we have 
int a[10]={5, 10, 2, 5, 50, 5, 10, 1, 2, 2};
the output should be 
number     frequency
5          3
10         2
2          3
50         1
1          1

#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
int a[10]={5, 10, 2, 5, 50, 5, 10, 1, 2, 2};
int flag[10]={0};
//count frequency of each number
int counter=1;
for(int i=0;i<=9;i++)
{
for(int j=i+1;j<=9;j++)
{
if (a[i]==a[j]){counter++;flag[j]=1;}
}
if(flag[i]!=1){cout<<a[i]<<"\t"<<counter<<endl;}
counter=1;
}
return 0;
}
view raw freq.cpp hosted with ❤ by GitHub

No comments:

Post a Comment