Simple java solution, O(N) time and O(1) space


  • 0
    Y
    public int removeDuplicates(int[] nums) {
        if(nums==null) return 0;
        if(nums.length<2) return nums.length;
        int m=nums[0]==nums[1]?0:1;
        for(int i=m;i<nums.length-1;i++){
            if(nums[i]!=nums[i+1]){
                nums[++m]=nums[i+1];
            }
        }
        return m+1;
    }

Log in to reply
 

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