---
# System prepended metadata

title: 0945. Minimum Increment to Make Array Unique
tags: [Leetcode, Medium]

---

# 0945. Minimum Increment to Make Array Unique
###### tags: `Leetcode` `Medium`
Link: https://leetcode.com/problems/minimum-increment-to-make-array-unique/
## 思路
先排序，再依次遍历数组元素，若当前元素小于等于它前一个元素，则将其变为前一个数+1。
## Code
```java=
class Solution {
    public int minIncrementForUnique(int[] nums) {
        Arrays.sort(nums);
        int res = 0;
        for(int i=1; i<nums.length; i++){
            if(nums[i]<=nums[i-1]){
                res += nums[i-1]+1-nums[i];
                nums[i] = nums[i-1];
            }
        }
        return res;
    }
}
```