Programming Examples
C program to find frequency of each element in one dimensional array
Write a program in ‘C’ language to input 20 arbitrary numbers in one-dimensional array. Calculate frequency of each number. Print the numbers and their frequency in a tabular form.
Solution
#include<stdio.h>
int main()
{
int num[20],a,b,flag,count;
printf("Enter any 20 Elements of Array : ");
for(a=0;a<20;a++)
{
scanf("%d",&num[a]);
}
printf("Frequency of Elements : \n");
for(a=0;a<20;a++)
{
count=0;
flag=0;
for(b=a-1;b>=0;b--)
{
if(num[a]==num[b])
{
flag=1;
break;
}
}
if(flag==0)
{
for(b=a;b<20;b++)
{
if(num[a]==num[b])
count++;
}
printf("%d\t%d\n",num[a],count);
}
}
return 0;
}
Output
Enter any 20 Elements of Array : 5
6
6
6
4
6
7
5
4
2
3
4
4
4
5
6
4
3
1
2
Frequency of Elements :
5 3
6 5
4 6
7 1
2 2
3 2
1 1