Simple Java Solution:)


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

Log in to reply
 

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