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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumOperations ( int a [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ a [ i ] ] += 1 ; int count = 0 ; for ( auto it = mp . begin ( ) ; it != mp . end ( ) ; it ++ ) { if ( ( * it ) . second > 1 ) count += ( * it ) . second - 1 ; } return count ; } int main ( ) { int a [ ] = { 2 , 1 , 2 , 3 , 3 , 4 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << minimumOperations ( a , n ) ; return 0 ; }