Remove Duplicate Numbers in Array

public class Solution {
    /**
     * @param nums an array of integers
     * @return the number of unique integers
     */
    public int deduplication(int[] nums) {
        // Write your code here
        if (nums.length == 0) {
            return 0;
        }
        
        Arrays.sort(nums);
        int i, j = -1;
        for (i = 0; i < nums.length; ++i) {
            if (j == -1 || nums[i] != nums[j]) {
                ++j;
                nums[j] = nums[i];
            }
        }
        
        return j + 1;
    }
}

最后更新于

这有帮助吗?