Minimum operations required to make all the elements distinct in an array | Function that returns minimum number of changes ; Hash - table to store frequency ; Increase the frequency of elements ; Traverse in the map to sum up the ( occurrences - 1 ) of duplicate elements ; Driver Code
function minimumOperations ( a , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . get ( a [ i ] ) != null ) { let x = mp . get ( a [ i ] ) ; mp . set ( a [ i ] , ++ x ) ; } else mp . set ( a [ i ] , 1 ) ; } let count = 0 ; for ( let [ key , value ] of mp . entries ( ) ) { if ( value > 1 ) { count += ( value - 1 ) ; } } return count ; } let a = [ 2 , 1 , 2 , 3 , 3 , 4 , 3 ] ; let n = a . length ; document . write ( minimumOperations ( a , n ) ) ;