Minimum removals required to make frequency of all remaining array elements equal | Function to count the minimum removals required to make frequency of all array elements equal ; Stores frequency of all array elements ; Traverse the array ; Stores all the frequencies ; Traverse the map ; Sort the frequencies ; Count of frequencies ; Stores the final count ; Traverse the vector ; Count the number of removals for each frequency and update the minimum removals required ; Print the final count ; Given array ; Size of the array ; Function call to print the minimum number of removals required
function minDeletions ( arr , N ) { var freq = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( freq . has ( arr [ i ] ) ) { freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . set ( arr [ i ] , 1 ) ; } } var v = [ ] ; freq . forEach ( ( value , key ) => { v . push ( value ) ; } ) ; v . sort ( ) ; var size = v . length ; var ans = N - ( v [ 0 ] * size ) ; for ( var i = 1 ; i < v . length ; i ++ ) { if ( v [ i ] != v [ i - 1 ] ) { var safe = v [ i ] * ( size - i ) ; ans = Math . min ( ans , N - safe ) ; } } document . write ( ans ) ; } var arr = [ 2 , 4 , 3 , 2 , 5 , 3 ] ; var N = arr . length ; minDeletions ( arr , N ) ;