C Program to print frequency of each element in array

Category: C Program
Tags: #array#cprogram

C Program to print the frequency of each element in an array without using an extra array. How to print the frequency of each element in an array without using an extra array.

C Program to print the frequency of each element in array

#include <stdio.h>

void main()
{
    int arr[50], size, pos, num, i, j, k, isCounted, count;

    printf("Enter number of elements: ");
    scanf("%d", &size);

    printf("Enter array elements-\n");
    for (i = 0; i < size; i++)
        scanf("%d", &arr[i]);

    printf("\nFrequency of elements-\n");
    for (i = 0; i < size; i++)
    {
        isCounted = 0;
        for (j = 0; j < i; j++)
        {
            if (arr[i] == arr[j])
                isCounted = 1;
        }
        if (!isCounted)
        {
            count = 0;
            for (k = i; k < size; k++)
            {
                if (arr[i] == arr[k])
                    count++;
            }
            printf("%d occurrs %d times\n", arr[i], count);
        }
    }
}

Output

Enter number of elements: 10

Enter array elements-
2
3
1
5
4
3
2
3
1
3

Frequency of elements-
2 occurrs 2 times
3 occurrs 4 times
1 occurrs 2 times
5 occurrs 1 times
4 occurrs 1 times