Write a program to count frequency of specific element in an array.

Write a program to count frequency of specific element in an array.

  1. #include <stdio.h>

  2. void main()
  3. {
  4.     int arr[100], freq[100];
  5.     int n, i, j, count;
  6.        printf("Input the number of elements to be stored in the array :");
  7.        scanf("%d", &n);
  8.    
  9.        printf("Input %d elements in the array :\n", n);
  10.        for(i=0; i<n; i++) {
  11.       printf("Element - %d : ",i);
  12.       scanf("%d", &arr[i]);
  13.   freq[i] = -1;
  14.     }
  15.     
  16.     for(i=0; i<n; i++) {
  17.         count = 1;
  18.         for(j=i+1; j<n; j++) {
  19.             if(arr[i] == arr[j]) {
  20.                 count++;
  21.                 freq[j] = 0;
  22.             }
  23.         }

  24.         if(freq[i] != 0) {
  25.             freq[i] = count;
  26.         }
  27.     }
  28.     
  29.     printf("\nThe frequency of all elements of array : \n");
  30.     for(i=0; i<n; i++) {
  31.         if(freq[i]!=0) {
  32.             printf("%d occurs %d times\n", arr[i], freq[i]);
  33.         }
  34.     }
  35. }

একটি মন্তব্য পোস্ট করুন