Remove elements from the array which appear more than k times | JavaScript program to remove the elements which appear more 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 less than or equal to k times . ; Driver code
function RemoveElements ( arr , n , k ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ; } for ( let i = 0 ; i < n ; ++ i ) { if ( mp . has ( arr [ i ] ) && 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 ) ;