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
#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:
- We declare an array
arr[]
with 8 elements. - The variable
size
holds the total number of elements, calculated usingsizeof(arr) / sizeof(arr[0])
. - An auxiliary array
freq[]
is initialized with-1
to store frequencies and mark counted elements. - The first loop iterates over the array elements.
- The inner loop counts occurrences of each element by comparing it with subsequent elements.
- Elements that have been counted are marked with
0
infreq[]
to avoid duplicate counting. - 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
#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:
- We declare an array
arr[]
with 8 elements. - A hash table
hash[MAX]
(size 100) is initialized with0
to store frequencies. - The first loop iterates through
arr[]
and incrementshash[arr[i]]
for each occurrence. - 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:
- Using Nested Loops: Suitable for small arrays but inefficient for large datasets.
- Using a Hash Table: More efficient, with linear time complexity
O(n)
.