Minimum changes required to make all element in an array equal | Java 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 map ; Traverse through map and find the maximum occurring element ; Return count of all element minus count of maximum occurring element ; Driver code ; Function call
import java . util . * ; class GFG { static int minChanges ( int arr [ ] , int n ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int maxElem = 0 ; for ( Map . Entry < Integer , Integer > entry : mp . entrySet ( ) ) { maxElem = Math . max ( maxElem , entry . getValue ( ) ) ; } return n - maxElem ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 3 , 4 } ; int n = arr . length ; System . out . println ( minChanges ( arr , n ) ) ; } }