Group multiple occurrence of array elements ordered by first occurrence | 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 ; Increment count of elements 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
def orderedGroup ( arr ) : NEW_LINE INDENT hM = { } NEW_LINE for i in range ( 0 , len ( arr ) ) : NEW_LINE INDENT hM [ arr [ i ] ] = hM . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( 0 , len ( arr ) ) : NEW_LINE INDENT count = hM . get ( arr [ i ] , None ) NEW_LINE if count != None : NEW_LINE INDENT for j in range ( 0 , count ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT del hM [ arr [ i ] ] NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 5 , 3 , 10 , 10 , 4 , 1 , 3 ] NEW_LINE orderedGroup ( arr ) NEW_LINE DEDENT