Minimum number of characters required to be removed such that every character occurs same number of times | C # program for the above approach ; 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 ; Driver Code ; Input
using System . Collections . Generic ; using System ; class GFG { static int minimumDeletion ( string s , int n ) { Dictionary < char , int > countMap = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( countMap . ContainsKey ( s [ i ] ) == true ) { countMap [ s [ i ] ] += 1 ; } else { countMap [ s [ i ] ] = 1 ; } } List < int > countMultiset = new List < int > ( ) ; foreach ( var values in countMap . Values ) { countMultiset . Add ( values ) ; } countMultiset . Sort ( ) ; int ans = 100000000 ; int index = 0 ; int m = countMultiset . Count ; foreach ( var j in countMultiset ) { ans = Math . Min ( ans , n - ( m - index ) * j ) ; index ++ ; } return ans ; } public static void Main ( String [ ] args ) { string S = " geeksforgeeks " ; int N = S . Length ; Console . WriteLine ( minimumDeletion ( S , N ) ) ; } }