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 code
using System ; using System . Collections . Generic ; class GFG { static int minDelete ( int [ ] arr , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( freq . ContainsKey ( arr [ i ] ) ) { freq [ arr [ i ] ] = freq [ arr [ i ] ] + 1 ; } else { freq . Add ( arr [ i ] , 1 ) ; } int max_freq = int . MinValue ; foreach ( KeyValuePair < int , int > entry in freq ) max_freq = Math . Max ( max_freq , entry . Value ) ; return n - max_freq + 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 3 , 4 , 4 , 2 , 4 } ; int n = arr . Length ; Console . Write ( minDelete ( arr , n ) ) ; } }