Given an unsorted array. The task is to calculate the cumulative frequency of each element of the array using count array.
Input : arr = [1, 2, 2, 1, 3, 4] Output :1->2 2->4 3->5 4->6 Input : arr = [1, 1, 1, 2, 2, 2] Output :1->3 2->6
A simple solution is to use two nested loops, the outer loops picks an element from left to right that are not visited. The inner loop counts its occurrences and mark occurrences as visited. Time complexity of this solution is O(n*n) and auxiliary space required is O(n).
A better solution is to use sorting. We sort the array so that same elements come together. After sorting, we linearly traverse elements and count their frequencies.
An efficient solution is to use hashing. Insert the element and its frequency in a set of pairs. As the set stores unique values in a sorted order, it will store all the elements with their frequencies in a sorted order. Iterate in the set and print the frequencies by adding the previous ones at every step.
Below is the implementation of the above approach.
1 2 2 4 3 5 4 6
Time complexity of the solution is O(n log n).
What if we need frequencies of elements according to the order of the first occurrence?
For example, an array [2, 4, 1, 2, 1, 3, 4], the frequency of 2 should be printed first, then of 4, then 1 and finally 3.
Approach: Hash the count of occurrences of an element. Traverse in the array and print the cumulative frequency. Once the element and its cumulative frequency has been printed, hash the occurrence of that element as 0 so that it not printed again if it appears in the latter half of array while traversal.
Below is the implementation of the above approach:
1->2 3->3 2->5 4->6
This article is contributed by Himanshu Ranjan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Replace each element by the difference of the total size of the array and frequency of that element
- k-th missing element in an unsorted array
- K'th Smallest/Largest Element in Unsorted Array | Set 1
- Find start and ending index of an element in an unsorted array
- Search an element in an unsorted array using minimum number of comparisons
- K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
- K'th Smallest/Largest Element in Unsorted Array | Set 3 (Worst Case Linear Time)
- Find the element having different frequency than other array elements
- Maximum element in an array which is equal to its frequency
- Sorting element of an array by frequency in decreasing order
- Frequency of each element of an array of small ranged values
- Find element in a sorted array whose frequency is greater than or equal to n/2.
- Find element in array with frequency equal to sum of frequencies of other elements
- Find frequency of each element in a limited range array in less than O(n) time
- Count pairs in an array such that frequency of one is at least value of other