Find frequency of each element in a limited range array in less than O ( n ) time | It prints number of occurrences of each element in the array . ; HashMap to store frequencies ; Traverse the array ; Update the frequency ; Traverse the hashmap ; Driver code
function findFrequency ( arr , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , 0 ) ; mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } for ( let [ key , value ] of mp . entries ( ) ) { document . write ( " " + key + " " + value + " " ) ; } } let arr = [ 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 ] ; let n = arr . length ; findFrequency ( arr , n ) ;