C program to count frequency of each element in an array (original) (raw)
Last Updated : 25 May, 2021
Given an array arr[] of size N, the task is to find the frequency of each distinct element present in the given array.
Examples:
Input: arr[] = { 1, 100000000, 3, 100000000, 3 }
Output: { 1 : 1, 3 : 2, 100000000 : 2 }
Explanation:
Distinct elements of the given array are { 1, 100000000, 3 }
Frequency of 1 in the given array is 1.
Frequency of 100000000 in the given array is 2.
Frequency of 3 in the given array is 2.
Therefore, the required output is { 1 : 1, 100000000 : 2, 3 : 2 }Input: arr[] = { 100000000, 100000000, 800000000, 100000000 }
Output: { 100000000 : 3, 800000000 : 1}
Approach: The problem can be solved using Binary search technique. Follow the steps below to solve the problem:
- Sort the array in ascending order.
- Traverse the array and for each distinct array element, find its lower_bound index and upper_bound indices using binary search and store in variables, say LB and UB respectively. Print the value of (UB - LB) as the frequency of that element.
Below is the implementation of the above approach:
C `
// C program to implement // the above approach
#include <stdio.h> #include <stdlib.h>
// Comparator function to sort // the array in ascending order int cmp(const void* a, const void* b) { return ((int)a - (int)b); }
// Function to find the lower_bound of X int lower_bound(int arr[], int N, int X) { // Stores minimum possible // value of the lower_bound int low = 0;
// Stores maximum possible
// value of the lower_bound
int high = N;
// Calculate the upper_bound
// of X using binary search
while (low < high) {
// Stores mid element
// of low and high
int mid = low + (high - low) / 2;
// If X is less than
// or equal to arr[mid]
if (X <= arr[mid]) {
// Find lower_bound in
// the left subarray
high = mid;
}
else {
// Find lower_bound in
// the right subarray
low = mid + 1;
}
}
// Return the lower_bound index
return low;
}
// Function to find the upper_bound of X int upper_bound(int arr[], int N, int X) { // Stores minimum possible // value of the upper_bound int low = 0;
// Stores maximum possible
// value of the upper_bound
int high = N;
// Calculate the upper_bound
// of X using binary search
while (low < high) {
// Stores mid element
// of low and high
int mid = low + (high - low) / 2;
// If X is greater than
// or equal to arr[mid]
if (X >= arr[mid]) {
// Find upper_bound in
// right subarray
low = mid + 1;
}
// If X is less than arr[mid]
else {
// Find upper_bound in
// left subarray
high = mid;
}
}
// Return the upper_bound index
return low;
}
// Function to find the frequency // of an element in the array int findFreq(int arr[], int N, int X) { // Stores upper_bound index of X int UB = upper_bound(arr, N, X);
// Stores lower_bound index of X
int LB = lower_bound(arr, N, X);
return (UB - LB);
}
// Utility function to print the frequency // of each distinct element of the array void UtilFindFreqArr(int arr[], int N) { // Sort the array in // ascending order qsort(arr, N, sizeof(int), cmp);
// Print start bracket
printf("{ ");
// Traverse the array
for (int i = 0; i < N;) {
// Stores frequency
// of arr[i];
int fr = findFreq(arr, N,
arr[i]);
// Print frequency of arr[i]
printf("%d : %d",
arr[i], fr);
// Update i
i++;
// Remove duplicate elements
// from the array
while (i < N && arr[i] == arr[i - 1]) {
// Update i
i++;
}
// If arr[i] is not
// the last array element
if (i <= N - 1) {
printf(", ");
}
}
// Print end bracket
printf(" }");
}
// Driver Code int main() { int arr[] = { 1, 100000000, 3, 100000000, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
UtilFindFreqArr(arr, N);
}
`
Output:
{ 1 : 1, 3 : 2, 100000000 : 2 }
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Similar Reads
- C Program to Find Common Array Elements Here, we will see how to find the common array elements using a C program. Input: a[6] = {1,2,3,4,5,6} b[6] = {5,6,7,8,9,10} Output: common array elements is 5 6 C // C program to find the common array elements #include <stdio.h> int main() { int a[6] = { 1, 2, 3, 4, 5, 6 }; int b[6] = { 5, 6, 1 min read
- C Program to Check for Majority Element in a sorted array Question: Write a function to find if a given integer x appears more than n/2 times in a sorted array of n integers. Basically, we need to write a function say isMajority() that takes an array (arr[] ), array’s size (n) and a number to be searched (x) as parameters and returns true if x is a majorit 6 min read
- C program to find the frequency of characters in a string Given a string S containing lowercase English characters, the task is to find the frequency of all the characters in the string. ExamplesInput: S="geeksforgeeks" Output: e - 4 f - 1 g - 2 k - 2 o - 1 r - 1 s - 2 Input: S="gfg" Output: f - 1 g - 2Approach Follow the steps to solve the problem: Initia 2 min read
- C Program to Calculate Average of an Array In this article, we will learn how to calculate the average of all elements of an array using a C program.The simplest method to calculate the average of all elements of an array is by using a loop. Let's take a look at an example:C#include <stdio.h> float getAvg(int arr[], int n) { int sum = 2 min read
- C program to count Positive and Negative numbers in an Array Given an array arr of integers of size N, the task is to find the count of positive numbers and negative numbers in the array Examples: Input: arr[] = {2, -1, 5, 6, 0, -3} Output: Positive elements = 3 Negative elements = 2 There are 3 positive, 2 negative, and 1 zero. Input: arr[] = {4, 0, -2, -9, 2 min read
- Frequency of an integer in the given array using Divide and Conquer Given an unsorted array arr[] and an integer K, the task is to count the occurrences of K in the given array using the Divide and Conquer method. Examples: Input: arr[] = {1, 1, 2, 2, 2, 2, 3}, K = 1 Output: 2 Input: arr[] = {1, 1, 2, 2, 2, 2, 3}, K = 4 Output: 0 Approach: The idea is to divide the 5 min read
- How to Find the Mode of Numbers in an Array in C? In C, the mode of array numbers is the element that appears most frequently in the array. To find the mode, we can count the occurrences of each element and identify the one with the highest count. In this article, we will find the mode of numbers in C. Example:Input: myArray = { 1, 2, 3, 4, 5, 2, 3 5 min read
- How to Find the Size of an Array in C? The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C.The simplest method to find the size of an array in C is by using sizeof operator. First determine t 2 min read
- size of char datatype and char array in C Given a char variable and a char array, the task is to write a program to find the size of this char variable and char array in C. Examples: Input: ch = 'G', arr[] = {'G', 'F', 'G'} Output: Size of char datatype is: 1 byte Size of char array is: 3 byte Input: ch = 'G', arr[] = {'G', 'F'} Output: Siz 2 min read
- C Program for Counting Sort Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence. Algorithm: Step 1: StartStep 2 : Find Larg 3 min read