Cumulative frequency of count of each element in an unsorted array | C ++ program to print the cumulative frequency according to the order given ; Function to print the cumulative frequency according to the order given ; Insert elements and their frequencies in hash map . ; traverse in the array ; add the frequencies ; if the element has not been visited previously ; mark the hash 0 as the element 's cumulative frequency  has been printed ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countFreq ( int a [ ] , int n ) { unordered_map < int , int > hm ; for ( int i = 0 ; i < n ; i ++ ) hm [ a [ i ] ] ++ ; int cumul = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cumul += hm [ a [ i ] ] ; if ( hm [ a [ i ] ] ) { cout << a [ i ] << " - > " << cumul << endl ; } hm [ a [ i ] ] = 0 ; } } int main ( ) { int a [ ] = { 1 , 3 , 2 , 4 , 2 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; countFreq ( a , n ) ; return 0 ; }