My accepted 8-line java solution with 2 pointers


  • 0
    E
    public int removeDuplicates(int[] nums) {
        if(nums == null) return 0;
        if(nums.length ==0||nums.length ==1) return nums.length;
        int curr = 0, start = 0;
        for(; curr < nums.length-1; ++curr){
            if(nums[curr] != nums[curr+1]) {
                if(start != curr) nums[++start] = nums[curr +1];
                else ++start;
            }
        }
        return start== curr? nums.length : ++ start;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.