# Cumulative frequency of count of each element in an unsorted array

Given an unsorted array. The task is to calculate the cumulative frequency of each element of the array using count array.

Examples:

```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
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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.

 `// C++ program to count cumlative ` `// frequencies of elements in an unsorted array. ` `#include ` `using` `namespace` `std; ` ` `  `void` `countFreq(``int` `a[], ``int` `n) ` `{ ` `    ``// Insert elements and their ` `    ``// frequencies in hash map. ` `    ``unordered_map<``int``, ``int``> hm; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``hm[a[i]]++; ` ` `  `    ``// Declare a set ` `    ``set > st; ` ` `  `    ``// insert the element and ` `    ``// and insert its frequency in a set ` `    ``for` `(``auto` `x : hm) { ` `        ``st.insert({ x.first, x.second }); ` `    ``} ` ` `  `    ``int` `cumul = 0; ` ` `  `    ``// iterate the set and print the ` `    ``// cumulative frequency ` `    ``for` `(``auto` `x : st) { ` `        ``cumul += x.second; ` `        ``cout << x.first << ``" "` `<< cumul << endl; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `a[] = { 1, 3, 2, 4, 2, 1 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]); ` `    ``countFreq(a, n); ` `    ``return` `0; ` `} `

 `// Java program to count cumlative  ` `// frequencies of elements in an unsorted array.  ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` `    ``static` `void` `countFreq(``int``[] a, ``int` `n)  ` `    ``{ ` ` `  `        ``// Insert elements and their ` `        ``// frequencies in hash map. ` `        ``HashMap hm = ``new` `HashMap<>(); ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``hm.put(a[i], hm.get(a[i]) == ``null` `?  ` `                     ``1` `: hm.get(a[i]) + ``1``); ` ` `  `        ``// Declare a Map ` `        ``SortedMap st = ``new` `TreeMap<>(); ` ` `  `        ``// insert the element and ` `        ``// and insert its frequency in a set ` `        ``for` `(HashMap.Entry x : hm.entrySet())  ` `        ``{ ` `            ``st.put(x.getKey(), x.getValue()); ` `        ``} ` ` `  `        ``int` `cumul = ``0``; ` ` `  `        ``// iterate the set and print the ` `        ``// cumulative frequency ` `        ``for` `(SortedMap.Entry x : st.entrySet()) ` `        ``{ ` `            ``cumul += x.getValue(); ` `            ``System.out.println(x.getKey() + ``" "` `+ cumul); ` `        ``} ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int``[] a = { ``1``, ``3``, ``2``, ``4``, ``2``, ``1` `}; ` `        ``int` `n = a.length; ` `        ``countFreq(a, n); ` `    ``} ` `} ` ` `  `// This code is contributed by ` `// sanjeev2552 `

 `# Python3 program to count cumlative  ` `# frequencies of elements in an unsorted array.  ` `def` `countFreq(a, n):  ` ` `  `    ``# Insert elements and their  ` `    ``# frequencies in hash map.  ` `    ``hm ``=` `{}  ` `    ``for` `i ``in` `range``(``0``, n):  ` `        ``hm[a[i]] ``=` `hm.get(a[i], ``0``) ``+` `1` ` `  `    ``# Declare a set  ` `    ``st ``=` `set``()  ` ` `  `    ``# Insert the element and  ` `    ``# its frequency in a set  ` `    ``for` `x ``in` `hm:  ` `        ``st.add((x, hm[x]))  ` ` `  `    ``cumul ``=` `0` ` `  `    ``# Iterate the set and print  ` `    ``# the cumulative frequency  ` `    ``for` `x ``in` `sorted``(st):  ` `        ``cumul ``+``=` `x[``1``]  ` `        ``print``(x[``0``], cumul)  ` ` `  `# Driver Code  ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``a ``=` `[``1``, ``3``, ``2``, ``4``, ``2``, ``1``]  ` `    ``n ``=` `len``(a)  ` `    ``countFreq(a, n) ` `     `  `# This code is contributed by Rituraj Jain `

 `// C# program to count cumlative  ` `// frequencies of elements in an ` `// unsorted array.  ` `using` `System; ` `using` `System.Collections.Generic;  ` `using` `System.Linq; ` ` `  `class` `GFG{ ` `     `  `static` `void` `countFreq(``int``[] a, ``int` `n)  ` `{ ` `     `  `    ``// Insert elements and their ` `    ``// frequencies in hash map. ` `    ``Dictionary<``int``, ` `               ``int``> hm = ``new` `Dictionary<``int``, ` `                                        ``int``>(); ` `    ``for``(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``if` `(hm.ContainsKey(a[i])) ` `        ``{ ` `            ``hm[a[i]]++; ` `        ``} ` `        ``else` `        ``{ ` `            ``hm[a[i]] = 1; ` `        ``} ` `    ``} ` ` `  `    ``int` `cumul = 0; ` ` `  `    ``// Iterate the set and print the ` `    ``// cumulative frequency ` `    ``foreach``(KeyValuePair<``int``,  ` `                         ``int``> x ``in` `hm.OrderBy(key => key.Key)) ` `    ``{ ` `        ``cumul += x.Value; ` `        ``Console.Write(x.Key + ``" "` `+ cumul + ``"\n"``); ` `    ``} ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(``string``[] args)  ` `{ ` `    ``int``[] a = { 1, 3, 2, 4, 2, 1 }; ` `    ``int` `n = a.Length; ` `     `  `    ``countFreq(a, n); ` `} ` `} ` ` `  `// This code is contributed by rutvik_56 `

Output:
```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:

 `// C++ program to print the cumulative frequency  ` `// according to the order given  ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print the cumulative frequency  ` `// according to the order given  ` `void` `countFreq(``int` `a[], ``int` `n) ` `{ ` `    ``// Insert elements and their  ` `    ``// frequencies in hash map. ` `    ``unordered_map<``int``, ``int``> hm; ` `    ``for` `(``int` `i=0; i"` `<< cumul << endl; ` `       ``} ` `       ``// mark the hash 0  ` `       ``// as the element's cumulative frequency  ` `       ``// has been printed  ` `       ``hm[a[i]]=0; ` `   ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `a[] = {1, 3, 2, 4, 2, 1}; ` `    ``int` `n = ``sizeof``(a)/``sizeof``(a[0]); ` `    ``countFreq(a, n); ` `    ``return` `0; ` `} `

 `// Java program to print the cumulative frequency  ` `// according to the order given  ` `class` `GFG ` `{ ` ` `  `// Function to print the cumulative frequency  ` `// according to the order given  ` `static` `void` `countFreq(``int` `a[], ``int` `n) ` `{ ` `    ``// Insert elements and their  ` `    ``// frequencies in hash map. ` `    ``int` `hm[] = ``new` `int``[n]; ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``hm[a[i]]++; ` `    ``int` `cumul = ``0``; ` `     `  `// traverse in the array  ` `for``(``int` `i = ``0``; i < n; i++) ` `{ ` `    ``// add the frequencies  ` `    ``cumul += hm[a[i]];  ` `         `  `    ``// if the element has not been  ` `    ``// visited previously  ` `    ``if``(hm[a[i]] != ``0``) ` `    ``{ ` `        ``System.out.println(a[i] + ``"->"` `+ cumul); ` `    ``} ` `    ``// mark the hash 0  ` `    ``// as the element's cumulative frequency  ` `    ``// has been printed  ` `    ``hm[a[i]] = ``0``; ` `} ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``int` `a[] = {``1``, ``3``, ``2``, ``4``, ``2``, ``1``}; ` `    ``int` `n = a.length; ` `    ``countFreq(a, n); ` `} ` `}  ` ` `  `// This code has been contributed by 29AjayKumar `

 `# Python3 program to print the cumulative  ` `# frequency according to the order given ` ` `  `# Function to print the cumulative frequency ` `# according to the order given ` `def` `countFreq(a, n): ` ` `  `    ``# Insert elements and their ` `    ``# frequencies in hash map. ` `    ``hm ``=` `dict``() ` `    ``for` `i ``in` `range``(n): ` `        ``hm[a[i]] ``=` `hm.get(a[i], ``0``) ``+` `1` ` `  `    ``cumul ``=` `0` ` `  `    ``# traverse in the array ` `    ``for` `i ``in` `range``(n): ` ` `  `    ``# add the frequencies ` `        ``cumul ``+``=` `hm[a[i]] ` ` `  `    ``# if the element has not been ` `    ``# visited previously ` `        ``if``(hm[a[i]] > ``0``): ` `            ``print``(a[i], ``"->"``, cumul) ` `             `  `    ``# mark the hash 0 ` `    ``# as the element's cumulative  ` `    ``# frequency has been printed ` `        ``hm[a[i]] ``=` `0` ` `  `# Driver Code ` `a ``=` `[``1``, ``3``, ``2``, ``4``, ``2``, ``1``] ` `n ``=` `len``(a) ` `countFreq(a, n) ` ` `  `# This code is contributed by mohit kumar `

 `// C# program to print the cumulative frequency  ` `// according to the order given  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` ` `  `// Function to print the cumulative frequency  ` `// according to the order given  ` `static` `void` `countFreq(``int` `[]a, ``int` `n)  ` `{  ` `    ``// Insert elements and their  ` `    ``// frequencies in hash map.  ` `    ``int` `[]hm = ``new` `int``[n];  ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `        ``hm[a[i]]++;  ` `    ``int` `cumul = 0;  ` `     `  `// traverse in the array  ` `for``(``int` `i = 0; i < n; i++)  ` `{  ` `    ``// add the frequencies  ` `    ``cumul += hm[a[i]];  ` `         `  `    ``// if the element has not been  ` `    ``// visited previously  ` `    ``if``(hm[a[i]] != 0)  ` `    ``{  ` `        ``Console.WriteLine(a[i] + ``"->"` `+ cumul);  ` `    ``}  ` `     `  `    ``// mark the hash 0  ` `    ``// as the element's cumulative frequency  ` `    ``// has been printed  ` `    ``hm[a[i]] = 0;  ` `}  ` `}  ` ` `  `// Driver Code  ` `public` `static` `void` `Main(String[] args)  ` `{  ` `    ``int` `[]a = {1, 3, 2, 4, 2, 1};  ` `    ``int` `n = a.Length;  ` `    ``countFreq(a, n);  ` `}  ` `}  ` ` `  `/* This code contributed by PrinciRaj1992 */`

Output:
```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 contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.