Write a program in C to find out the frequency of elements in an array
#include <stdio.h>
int main()
{
int i, j, n, temp, cnt;
printf("Enter n: ");
scanf("%d", &n);
int array[n];
printf("Enter array: \n");
for (int i=0; i<n; i++)
scanf("%d", &array[i]);
printf("\nArray:\n");
for (i=0; i<n; i++)
printf("%d ", array[i]);
for (i=0; i<n-1; i++)
for (j=i+1; j<n; j++)
if (array[i]> array[j])
{
temp = array[i];
array[i]= array[j];
array[j] = temp;
}
for (i=0; i<n; i++)
{
cnt = 0;
for (j=i; j<n; j++)
if(array[i] == array[j])
cnt++;
else break;
printf("\nElement: %d frequency: %d", array[i], cnt);
i = j-1;
}
}
Comments
Leave a comment