Find the minimum number of operations required to make all array elements equal | C # implementation of the above approach ; Function to return the minimum operations required to make all array elements equal ; To store the frequency of all the array elements ; Traverse through array elements and update frequencies ; To store the maximum frequency of an element from the array ; Traverse through the map and find the maximum frequency for any element ; Return the minimum operations required ; Driver code
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static int minOperations ( 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 maxFreq = int . MinValue ; maxFreq = m . Values . Max ( ) ; return ( n - maxFreq ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 6 } ; int n = arr . Length ; Console . WriteLine ( minOperations ( arr , n ) ) ; } }