Minimum delete operations to make all elements of array same | C ++ program to find minimum number of deletes required to make all elements same . ; Function to get minimum number of elements to be deleted from array to make array elements equal ; Create an hash map and store frequencies of all array elements in it using element as key and frequency as value ; Find maximum frequency among all frequencies . ; To minimize delete operations , we remove all elements but the most frequent element . ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDelete ( int arr [ ] , int n ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; int max_freq = INT_MIN ; for ( auto itr = freq . begin ( ) ; itr != freq . end ( ) ; itr ++ ) max_freq = max ( max_freq , itr -> second ) ; return n - max_freq ; } int main ( ) { int arr [ ] = { 4 , 3 , 4 , 4 , 2 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minDelete ( arr , n ) ; return 0 ; }