Minimum characters required to be removed to make frequency of each character unique | 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
function minCntCharDeletionsfrequency ( str , N ) { let mp = new Map ( ) ; let pq = [ ] ; let cntChar = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( mp . has ( str [ i ] ) ) { mp . set ( str [ i ] , mp . get ( str [ i ] ) + 1 ) ; } else { mp . set ( str [ i ] , 1 ) ; } } for ( let [ key , value ] of mp . entries ( ) ) { pq . push ( value ) ; } pq . sort ( function ( a , b ) { return b - a ; } ) ; while ( pq . length != 0 ) { let frequent = pq [ 0 ] ; pq . shift ( ) ; if ( pq . length == 0 ) { return cntChar ; } if ( frequent == pq [ 0 ] ) { if ( frequent > 1 ) { pq . push ( frequent - 1 ) ; pq . sort ( function ( a , b ) { return b - a ; } ) ; } cntChar ++ ; } } return cntChar ; } let str = " " ; let N = str . length ; document . write ( minCntCharDeletionsfrequency ( str . split ( " " ) , N ) ) ;