Group multiple occurrence of array elements ordered by first occurrence | C # program to group multiple occurrences of individual array elements ; A hashing based method to group all occurrences of individual elements ; Creates an empty hashmap ; Traverse the array elements , and store count for every element in HashMap ; Check if element is already in HashMap ; Increment count of element element in HashMap ; Traverse array again ; Check if this is first occurrence ; If yes , then print the element ' count ' times ; And remove the element from HashMap . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void orderedGroup ( int [ ] arr ) { Dictionary < int , int > hM = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { int prevCount = 0 ; if ( hM . ContainsKey ( arr [ i ] ) ) prevCount = hM [ arr [ i ] ] ; if ( hM . ContainsKey ( arr [ i ] ) ) hM [ arr [ i ] ] = prevCount + 1 ; else hM . Add ( arr [ i ] , prevCount + 1 ) ; } for ( int i = 0 ; i < arr . Length ; i ++ ) { int count = 0 ; if ( hM . ContainsKey ( arr [ i ] ) ) count = hM [ arr [ i ] ] ; if ( count != 0 ) { for ( int j = 0 ; j < count ; j ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; hM . Remove ( arr [ i ] ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 3 , 10 , 10 , 4 , 1 , 3 } ; orderedGroup ( arr ) ; } }