Minimum operations required to make all the elements distinct in an array | C # program to find Minimum number of changes to make array distinct ; 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
using System ; using System . Collections . Generic ; class geeks { public static int minimumOperations ( int [ ] a , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) ) { var val = mp [ a [ i ] ] ; mp . Remove ( a [ i ] ) ; mp . Add ( a [ i ] , val + 1 ) ; } else { mp . Add ( a [ i ] , 1 ) ; } } int count = 0 ; foreach ( KeyValuePair < int , int > entry in mp ) { if ( entry . Value > 1 ) { count += ( entry . Value - 1 ) ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 1 , 2 , 3 , 3 , 4 , 3 } ; int n = a . Length ; Console . WriteLine ( minimumOperations ( a , n ) ) ; } }