Minimum number of characters required to be removed such that every character occurs same number of times | Function to find minimum number of character removals required to make frequency of all distinct characters the same ; Stores the frequency of each character ; Traverse the string ; Stores the frequency of each charachter in sorted order ; Traverse the Map ; Insert the frequency in multiset ; Stores the count of elements required to be removed ; Stores the size of multiset ; Traverse the multiset ; Update the ans ; Increment i by 1 ; Return ; Input
function minimumDeletion ( s , n ) { var countMap = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( countMap . has ( s [ i ] ) ) { countMap . set ( s [ i ] , countMap . get ( s [ i ] ) + 1 ) ; } else { countMap . set ( s [ i ] , 1 ) ; } } var countMultiset = [ ] ; countMap . forEach ( ( value , key ) => { countMultiset . push ( value ) ; } ) ; countMultiset . sort ( ) ; var ans = 1000000000 ; var i = 0 ; var m = countMultiset . length ; countMultiset . forEach ( j => { ans = Math . min ( ans , n - ( m - i ) * j ) ; i ++ ; } ) ; return ans ; } var S = " " ; var N = S . length ; document . write ( minimumDeletion ( S , N ) ) ;