Group multiple occurrence of array elements ordered by first occurrence | A simple method to group all occurrences of individual elements ; Initialize all elements as not visited ; Traverse all elements ; Check if this is first occurrence ; If yes , prlet it and all subsequent occurrences ; Driver program
function groupElements ( arr , n ) { let visited = Array ( n ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { visited [ i ] = false ; } for ( let i = 0 ; i < n ; i ++ ) { if ( ! visited [ i ] ) { document . write ( arr [ i ] + " " ) ; for ( let j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { document . write ( arr [ i ] + " " ) ; visited [ j ] = true ; } } } } } let arr = [ 4 , 6 , 9 , 2 , 3 , 4 , 9 , 6 , 10 , 4 ] ; let n = arr . length ; groupElements ( arr , n ) ;