Sort an array of 0 s , 1 s and 2 s ( Simple Counting ) | Example input = [ 0 , 1 , 2 , 2 , 0 , 0 ] output = [ 0 , 0 , 0 , 1 , 2 , 2 ]
let inputArray = [ 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ] ; let outputArray = [ ] ; let indexOfOne = 0 ; function print ( ) { for ( let item of inputArray . values ( ) ) if ( item == 2 ) outputArray . push ( item ) ; else if ( item == 1 ) { outputArray . splice ( indexOfOne , 0 , item ) ; indexOfOne += 1 ; } else if ( item == 0 ) { outputArray . splice ( 0 , 0 , item ) ; indexOfOne += 1 ; } else { document . write ( " " ) ; continue ; } } print ( ) ; for ( let item of outputArray . values ( ) ) document . write ( item + " " ) ;