Count the occurrences of elements in an array
This is the program of counting the occurrences of each of the elements in an array asked in interview.I used a sample array for this program.
#include<stdio.h>
#include<conio.h>
int main()
{
int k=0,count=0,z=0;
int countarr[16],nos[16];
int arr[20]={1,1,23,23,23,34,34,34,45,45,33,33,33,33,33,33,54,54,11,12};
for(int i=0;i<20;i++)
{
if(i>0)
{
i=i+count-1;
}
nos[z]=arr[i];
z=z+1;
count=0;
for(int j=0;j<20;j++)
{
if(arr[i]==arr[j])
count++;
}
countarr[k]=count;
k=k+1;
}
printf("The count of each element of the array is :\n");
for(int i=0;i<k;i++)
printf("%d-%d\n",nos[i],countarr[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
int main()
{
int k=0,count=0,z=0;
int countarr[16],nos[16];
int arr[20]={1,1,23,23,23,34,34,34,45,45,33,33,33,33,33,33,54,54,11,12};
for(int i=0;i<20;i++)
{
if(i>0)
{
i=i+count-1;
}
nos[z]=arr[i];
z=z+1;
count=0;
for(int j=0;j<20;j++)
{
if(arr[i]==arr[j])
count++;
}
countarr[k]=count;
k=k+1;
}
printf("The count of each element of the array is :\n");
for(int i=0;i<k;i++)
printf("%d-%d\n",nos[i],countarr[i]);
getch();
}
Comments
Post a Comment