Minimum characters required to be removed to make frequency of each character unique | C # program to implement the above approach ; Function to find the minimum count of characters required to be deleted to make frequencies of all characters unique ; Stores frequency of each distinct character of str ; Store frequency of each distinct character such that the largest frequency is present at the top ; Stores minimum count of characters required to be deleted to make frequency of each character unique ; Traverse the String ; Update frequency of str [ i ] ; Traverse the map ; Insert current frequency into pq ; Traverse the priority_queue ; Stores topmost element of pq ; Pop the topmost element ; If pq is empty ; Return cntChar ; If frequent and topmost element of pq are equal ; If frequency of the topmost element is greater than 1 ; Insert the decremented value of frequent ; Update cntChar ; Driver Code ; Stores length of str
using System ; using System . Collections . Generic ; class GFG { static int minCntCharDeletionsfrequency ( char [ ] str , int N ) { Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; List < int > pq = new List < int > ( ) ; int cntChar = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( str [ i ] ) ) { mp [ str [ i ] ] ++ ; } else { mp . Add ( str [ i ] , 1 ) ; } } foreach ( KeyValuePair < char , int > it in mp ) { pq . Add ( it . Value ) ; } pq . Sort ( ) ; pq . Reverse ( ) ; while ( pq . Count != 0 ) { pq . Sort ( ) ; pq . Reverse ( ) ; int frequent = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; if ( pq . Count == 0 ) { return cntChar ; } if ( frequent == pq [ 0 ] ) { if ( frequent > 1 ) { pq . Add ( frequent - 1 ) ; pq . Sort ( ) ; pq . Reverse ( ) ; } cntChar ++ ; } } return cntChar ; } public static void Main ( String [ ] args ) { String str = " abbbcccd " ; int N = str . Length ; Console . Write ( minCntCharDeletionsfrequency ( str . ToCharArray ( ) , N ) ) ; } }