Find array elements with frequencies in range [ l , r ] | Javascript program to find the elements whose frequency lies in the range [ l , r ] ; Hash map which will store the frequency of the elements of the array . ; Increment the frequency of the element by 1. ; Print the element whose frequency lies in the range [ l , r ] ; Driver Code
function findElements ( arr , n , l , r ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { let a = 0 ; if ( mp . get ( arr [ i ] ) == null ) { a = 1 ; } else { a = mp . get ( arr [ i ] ) + 1 ; } mp . set ( arr [ i ] , a ) ; } for ( let i = 0 ; i < n ; ++ i ) { if ( l <= mp . get ( arr [ i ] ) && ( mp . get ( arr [ i ] ) <= r ) ) { document . write ( arr [ i ] + " " ) ; } } } let arr = [ 1 , 2 , 3 , 3 , 2 , 2 , 5 ] ; let n = arr . length ; let l = 2 , r = 3 ; findElements ( arr , n , l , r ) ;