Minimum changes required to make all element in an array equal | C # program to find minimum changes required to make all elements of the array equal ; Function to count of minimum changes required to make all elements equal ; Store the count of each element as key - value pair in Dictionary ; Traverse through the Dictionary and find the maximum occurring element ; Return count of all element minus count of maximum occurring element ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static int minChanges ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { var val = mp [ arr [ i ] ] ; mp . Remove ( arr [ i ] ) ; mp . Add ( arr [ i ] , val + 1 ) ; } else { mp . Add ( arr [ i ] , 1 ) ; } } int maxElem = 0 ; foreach ( KeyValuePair < int , int > entry in mp ) { maxElem = Math . Max ( maxElem , entry . Value ) ; } return n - maxElem ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( minChanges ( arr , n ) ) ; } }