Minimum number of distinct elements after removing M items | Set 2 | Function to return minimum distinct character after M removals ; Count the occurences of number and store in count ; Count the occurences of the frequencies ; Take answer as total unique numbers and remove the frequency and subtract the answer ; Remove the minimum number of times ; Return the answer ; Initialize array ; Size of array ; Function call
function distinctNumbers ( arr , m , n ) { var count = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) if ( count . has ( arr [ i ] ) ) count . set ( arr [ i ] , count . get ( arr [ i ] ) + 1 ) else count . set ( arr [ i ] , 1 ) var fre_arr = Array ( n + 1 ) . fill ( 0 ) ; count . forEach ( ( value , key ) => { fre_arr [ value ] ++ ; } ) ; var ans = count . size ; for ( var i = 1 ; i <= n ; i ++ ) { var temp = fre_arr [ i ] ; if ( temp == 0 ) continue ; var t = Math . min ( temp , parseInt ( m / i ) ) ; ans -= t ; m -= i * t ; } return ans ; } var arr = [ 2 , 4 , 1 , 5 , 3 , 5 , 1 , 3 ] ; var n = arr . length ; var m = 2 ; document . write ( distinctNumbers ( arr , m , n ) ) ;