Minimum removals required to make frequency of all remaining array elements equal | C ++ program for the above approach ; Function to count the minimum removals required to make frequency of all array elements equal ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDeletions ( int arr [ ] , int N ) { map < int , int > freq ; for ( int i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } vector < int > v ; for ( auto z : freq ) { v . push_back ( z . second ) ; } sort ( v . begin ( ) , v . end ( ) ) ; int size = v . size ( ) ; int ans = N - ( v [ 0 ] * size ) ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) { if ( v [ i ] != v [ i - 1 ] ) { int safe = v [ i ] * ( size - i ) ; ans = min ( ans , N - safe ) ; } } cout << ans ; } int main ( ) { int arr [ ] = { 2 , 4 , 3 , 2 , 5 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minDeletions ( arr , N ) ; }