Minimum Increment to Make Array Unique - Problem

You are given an integer array nums. In one move, you can pick an index i where 0 <= i < nums.length and increment nums[i] by 1.

Return the minimum number of moves to make every value in nums unique.

The test cases are generated so that the answer fits in a 32-bit integer.

Input & Output

Example 1 — Basic Duplicates
$ Input: nums = [1,2,2]
Output: 1
💡 Note: One of the 2's needs to be incremented to 3. Total moves: 1 (change second 2 to 3).
Example 2 — Multiple Duplicates
$ Input: nums = [3,1,6,2,2,2]
Output: 3
💡 Note: After sorting: [1,2,2,2,3,6]. Second 2→3 (1 move), third 2→4 (2 moves). Total: 3 moves.
Example 3 — Already Unique
$ Input: nums = [1,2,3]
Output: 0
💡 Note: All elements are already unique, no moves needed.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 0 ≤ nums[i] ≤ 105

Visualization

Tap to expand
Minimum Increment to Make Array Unique: [1,2,2] → [1,2,3]122Input Array (has duplicates)Increment duplicate123Result Array (all unique)Minimum Moves: 1 (second 2 becomes 3)
Understanding the Visualization
1
Input
Array with duplicate values that need to be made unique
2
Process
Sort array and increment duplicates to next available values
3
Output
Minimum number of increment operations needed
Key Takeaway
🎯 Key Insight: Sort first to group duplicates, then ensure each element is at least previous + 1
Asked in
Google 15 Amazon 12 Meta 8 Apple 6
28.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen