Minimum operation to make all elements equal in array | C # Code For Minimum operation to make all elements equal in array ; function for min operation ; Insert all elements in hash . ; find the max frequency ; return result ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static int minOperation ( int [ ] arr , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( arr [ i ] ) ) { var val = m [ arr [ i ] ] ; m . Remove ( arr [ i ] ) ; m . Add ( arr [ i ] , val + 1 ) ; } else { m . Add ( arr [ i ] , 1 ) ; } } int max_count = 0 ; HashSet < int > s = new HashSet < int > ( m . Keys ) ; foreach ( int i in s ) if ( max_count < m [ i ] ) max_count = m [ i ] ; return ( n - max_count ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = arr . Length ; Console . Write ( minOperation ( arr , n ) ) ; } }