Java Code with O(1) extra memory.


  • 0
    J
     public int removeDuplicates(int[] nums) {
            if (nums.length<2){
                return nums.length;
            }
            int length = 1;
            int j = 0;
            while(j<nums.length-1){
                if (nums[j]!=nums[j+1]){
                     nums[length]=nums[j+1];
                    length++;
                }
                j++;
            }
            return length;
        }
    }
    

Log in to reply
 

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