Find frequency of each element in a limited range array in less than O ( n ) time | Java program to count number of occurrences of each element in the array in O ( n ) time and O ( 1 ) space ; check if the current element is equal to previous element . ; reset the frequency ; print the last element and its frequency ; Driver code
function void findFrequencies ( ele ) { var freq = 1 ; var idx = 1 ; var element = ele [ 0 ] ; while ( idx < ele . length ) { if ( ele [ idx - 1 ] == ele [ idx ] ) { freq ++ ; idx ++ ; } else { document . write ( element + " ▁ " + freq + " < br > " ) ; element = ele [ idx ] ; idx ++ ; freq = 1 ; } } document . write ( element + " ▁ " + freq + " < br > " ) ; } document . write ( " - - - frequencies ▁ in ▁ a ▁ sorted ▁ array - - - - " + " < br > " ) ; findFrequencies ( new Array ( 10 , 20 , 30 , 30 , 30 , 40 , 50 , 50 , 50 , 50 , 70 ) ) ;