Two pointers method


  • 0
    public class Solution {
        public int removeDuplicates(int[] nums) {
            if(nums.length < 2)
                 return nums.length;
            int pos = 0;
            for(int i = 1; i < nums.length; i++){
                if(nums[pos] != nums[i])
                    nums[++pos] = nums[i];
            }
            return pos+1;
        }
    }
    

Log in to reply
 

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