Maximizing the elements with a [ i + 1 ] > a [ i ] | returns the number of positions where A ( i + 1 ) is greater than A ( i ) after rearrangement of the array ; Creating a HashMap containing char as a key and occurrences as a value ; Find the maximum frequency ; Driver code
function countMaxPos ( arr ) { let n = arr . length ; let map = new Map ( ) ; for ( let x of arr ) { if ( map . has ( x ) ) map . set ( x , map . get ( x ) + 1 ) ; else map . set ( x , 1 ) ; } let max_freq = 0 ; for ( let entry of map ) max_freq = Math . max ( max_freq , entry [ 1 ] ) ; console . log ( max_freq , n ) return n - max_freq ; } let arr = [ 20 , 30 , 10 , 50 , 40 ] ; document . write ( countMaxPos ( arr ) ) ;