text: Minimum removals required to make frequency of each array element equal to its value | Function to find the minimum count of elements required to be removed such that frequency of arr [ i ] equal to arr [ i ] ; Stores frequency of each element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores minimum count of removals ; Traverse the map ; Stores key value of the map ; If frequency of i is less than i ; Update cntMinRem ; If frequency of i is greater than i ; Update cntMinRem ; Driver Code
code: function min_elements ( arr , N ) { var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } var cntMinRem = 0 ; mp . forEach ( ( value , key ) => { var i = key ; if ( mp . get ( i ) < i ) { cntMinRem += mp . get ( i ) ; } else if ( mp . get ( i ) > i ) { cntMinRem += ( mp . get ( i ) - i ) ; } } ) ; return cntMinRem ; } var arr = [ 2 , 4 , 1 , 4 , 2 ] ; var N = arr . length ; document . write ( min_elements ( arr , N ) ) ;