Find the Frequency of Elements in an Array in C

To find the frequency of elements in an array in C, we iterate through the array and count the occurrences of each element. We can achieve this using nested loops, hash tables, or sorting techniques, depending on efficiency requirements.

In this tutorial, we will explore different ways to find the frequency of elements in an array with detailed explanations and examples.


Examples to Find Frequency of Elements

1. Using Nested Loops to Count Frequency

In this example, we will use nested loops to count the frequency of each element in the array. We will iterate through the array and compare each element with the rest to count occurrences.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 2, 1, 3, 4, 1};
    int size = sizeof(arr) / sizeof(arr[0]);
    int freq[size];

    // Initialize frequency array with -1
    for (int i = 0; i < size; i++) {
        freq[i] = -1;
    }

    // Counting frequency
    for (int i = 0; i < size; i++) {
        int count = 1;
        for (int j = i + 1; j < size; j++) {
            if (arr[i] == arr[j]) {
                count++;
                freq[j] = 0; // Mark element as counted
            }
        }
        if (freq[i] != 0) {
            freq[i] = count;
        }
    }

    // Printing the frequency of each element
    printf("Element | Frequency\n");
    for (int i = 0; i < size; i++) {
        if (freq[i] != 0) {
            printf("   %d    |   %d\n", arr[i], freq[i]);
        }
    }

    return 0;
}

Explanation:

  1. We declare an array arr[] with 8 elements.
  2. The variable size holds the total number of elements, calculated using sizeof(arr) / sizeof(arr[0]).
  3. An auxiliary array freq[] is initialized with -1 to store frequencies and mark counted elements.
  4. The first loop iterates over the array elements.
  5. The inner loop counts occurrences of each element by comparing it with subsequent elements.
  6. Elements that have been counted are marked with 0 in freq[] to avoid duplicate counting.
  7. Finally, we print each unique element along with its frequency.

Output:

Element | Frequency
   1    |   3
   2    |   2
   3    |   2
   4    |   1

2. Using a Hash Table (Efficient Approach)

In this example, we use a hash table (array-based) approach to store and count element frequencies efficiently.

main.c

</>
Copy
#include <stdio.h>

#define MAX 100

int main() {
    int arr[] = {5, 7, 5, 9, 7, 5, 9, 9};
    int size = sizeof(arr) / sizeof(arr[0]);
    int hash[MAX] = {0};

    // Counting frequency
    for (int i = 0; i < size; i++) {
        hash[arr[i]]++;
    }

    // Printing frequency of elements
    printf("Element | Frequency\n");
    for (int i = 0; i < MAX; i++) {
        if (hash[i] > 0) {
            printf("   %d    |   %d\n", i, hash[i]);
        }
    }

    return 0;
}

Explanation:

  1. We declare an array arr[] with 8 elements.
  2. A hash table hash[MAX] (size 100) is initialized with 0 to store frequencies.
  3. The first loop iterates through arr[] and increments hash[arr[i]] for each occurrence.
  4. The second loop prints non-zero frequencies, showing each element and its count.

Output:

Element | Frequency
   5    |   3
   7    |   2
   9    |   3

Conclusion

In this tutorial, we explored different ways to find the frequency of elements in an array:

  1. Using Nested Loops: Suitable for small arrays but inefficient for large datasets.
  2. Using a Hash Table: More efficient, with linear time complexity O(n).