Skip to main content
Back to problems
#945
Medium Algorithms

Minimum increment to make array unique

Array Greedy Sorting Counting
60.6% acceptance
Feb 25, 2026
2775
85
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.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn min_increment_for_unique(nums: Vec<i32>) -> i32 {
    let mut nums = nums;
    nums.sort();
    let mut moves = 0;
    for i in 1..nums.len() {
      if nums[i] <= nums[i-1] {
        let diff = nums[i-1] + 1 - nums[i];
        moves += diff;
        nums[i] = nums[i-1] + 1;
      }
    }
    moves
  }
}