Remove elements that appear strictly less than k times | JavaScript program to remove the elements which appear strictly less than k times from the array . ; Hash map which will store the frequency of the elements of the array . ; Incrementing the frequency of the element by 1. ; Print the element which appear more than or equal to k times . ; Driver code
function removeElements ( arr , n , k ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { if ( ! mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , 1 ) ; else { let x = mp . get ( arr [ i ] ) ; mp . set ( arr [ i ] , ++ x ) ; } } for ( let i = 0 ; i < n ; ++ i ) { if ( mp . get ( arr [ i ] ) >= k ) document . write ( arr [ i ] + " " ) ; } } let arr = [ 1 , 2 , 2 , 3 , 2 , 3 , 4 ] ; let n = arr . length ; let k = 2 ; removeElements ( arr , n , k ) ;