Minimum removals required to make frequency of all remaining array elements equal | Java program for the above approach ; Stores frequency of all array elements ; Traverse the array ; Stores all the frequencies ; Traverse the map ; Sort the frequencies ; Count of frequencies ; Stores the final count ; Traverse the vector ; Count the number of removals for each frequency and update the minimum removals required ; Print the final count ; Driver code ; Given array ; Size of the array ; Function call to print the minimum number of removals required
import java . io . * ; import java . util . HashMap ; import java . util . Map ; import java . util . ArrayList ; import java . util . Collections ; class GFG { public static void minDeletions ( int arr [ ] , int N ) { HashMap < Integer , Integer > map = new HashMap < > ( ) ; ; for ( int i = 0 ; i < N ; i ++ ) { Integer k = map . get ( arr [ i ] ) ; map . put ( arr [ i ] , ( k == null ) ? 1 : k + 1 ) ; } ArrayList < Integer > v = new ArrayList < > ( ) ; for ( Map . Entry < Integer , Integer > e : map . entrySet ( ) ) { v . add ( e . getValue ( ) ) ; } Collections . sort ( v ) ; int size = v . size ( ) ; int ans = N - ( v . get ( 0 ) * size ) ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) { if ( v . get ( i ) != v . get ( i - 1 ) ) { int safe = v . get ( i ) * ( size - i ) ; ans = Math . min ( ans , N - safe ) ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 3 , 2 , 5 , 3 } ; int N = 6 ; minDeletions ( arr , N ) ; } }