C Program to Count Each Elements In Array

Below is a simple C program that counts the occurrences of each element in an array. This program takes an array as input from the user, counts the occurrences of each element, and then prints the count for each unique element in the array. It uses a nested loop to iterate through the array and count occurrences.

Program :

#include <stdio.h>
int main()
{
int arr[100], freq[100];
int size, i, j, count;
printf(“Enter size of array: “);
scanf(“%d”, &size);
printf(“Enter elements in array: “);
for(i=0; i<size; i++)
{
scanf(“%d”, &arr[i]);
freq[i] = -1;
}
for(i=0; i<size; i++)
{
count = 1;
for(j=i+1; j<size; j++)
{
if(arr[i]==arr[j])
{
count++;
freq[j] = 0;
}
}
if(freq[i] != 0)
{
freq[i] = count;
}
}
printf(“\nFrequency of all elements of array : \n”);
for(i=0; i<size; i++)
{
if(freq[i] != 0)
{
printf(“%d occurs %d times\n”, arr[i], freq[i]);
}
}
return 0;
}

Output :

Shopping Basket
Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare
Verified by MonsterInsights