Tuesday, January 17, 2023

C Programming. Frequency of each element in an array.

 Frequency of each element in an array.


#include<stdio.h>
int main() {
    int a[10];
    int i, j, c=0;
   
    printf("Enter the elements of an array : \n");
    for (i=0; i<10; i++){
        scanf("%d",&a[i]);
    }

    for (i=0; i<10; i++){
        c=1;
        if (a[i]!=-1){
            for (j=i+1; j<10; j++){
                if(a[i]==a[j]){
                    c++;
                    a[j]=-1;
                }
            }
            printf("Frequency of %d is %d.\n",a[i],c);
        }
    }

    return 0;
}  

Output:-

Enter the elements of an array : 1 2 1 3 6 5 2 4 1 8 Frequency of 1 is 3. Frequency of 2 is 2. Frequency of 3 is 1. Frequency of 6 is 1. Frequency of 5 is 1. Frequency of 4 is 1. Frequency of 8 is 1.

No comments:

Post a Comment

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the un...