Java Time O(n) Space O(1)


  • 0
    G
    class Solution {
        public int removeDuplicates(int[] nums) {
            if (nums == null || nums.length == 0) {
                return 0;
            }
            int index = 0, size = 1;
            for (int num : nums) {
                if (num != nums[index]) {
                    nums[++index] = num;
                    size++;
                }
            }
            return size;
        }
    }
    

Log in to reply
 

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