# Minimum operations required to make all the elements distinct in an array

Given an array of N integers. If a number occurs more than once, choose any number y from the array and replace the x in the array to x+y such that x+y is not in the array. The task is to find the minimum number of operations to make the array a distinct one.

**Examples:**

Input:a[] = {2, 1, 2}

Output:1

Let x = 2, y = 1 then replace 2 by 3.

Performing the above step makes all the elements in the array distinct.

Input:a[] = {1, 2, 3}

Output:0

**Approach:** If a number appears more than once, then the summation of (occurrences-1) for all duplicate elements will be the answer. The main logic behind this is if x is replaced by x+y where y is the largest element in the array, then x is replaced by x+y which is the largest element in the array. Use a map to store the frequency of the numbers of array. Traverse in the map, and if the frequency of an element is more than 1, add it to the count by subtracting one.

Below is the implementation of the above approach:

`// C++ program to find Minimum number ` `// of changes to make array distinct ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Fucntion that returns minimum number of changes ` `int` `minimumOperations(` `int` `a[], ` `int` `n) ` `{ ` ` ` ` ` `// Hash-table to store frequency ` ` ` `unordered_map<` `int` `, ` `int` `> mp; ` ` ` ` ` `// Increase the frequency of elements ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `mp[a[i]] += 1; ` ` ` ` ` `int` `count = 0; ` ` ` ` ` `// Traverse in the map to sum up the (occurences-1) ` ` ` `// of duplicate elements ` ` ` `for` `(` `auto` `it = mp.begin(); it != mp.end(); it++) { ` ` ` `if` `((*it).second > 1) ` ` ` `count += (*it).second-1; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 2, 1, 2, 3, 3, 4, 3 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `cout << minimumOperations(a, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(N)

## Recommended Posts:

- Minimum operations required to make all the array elements equal
- Find the number of operations required to make all array elements Equal
- Minimum gcd operations to make all array elements one
- Minimum operations of the given type required to make a complete graph
- Minimum number of changes required to make the given array an AP
- Minimum operations to make GCD of array a multiple of k
- Minimum Increment operations to make Array unique
- Minimum operations to make counts of remainders same in an array
- Minimum array insertions required to make consecutive difference <= K
- Minimum increment operations to make the array in increasing order
- Find minimum operations needed to make an Array beautiful
- Find minimum number of merge operations to make an array palindrome
- Operations required to make the string empty
- Minimum number of increment/decrement operations such that array contains all elements from 1 to N
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.