Skip to content

Latest commit

 

History

History
36 lines (27 loc) · 695 Bytes

Remove Duplicates from Sorted Array.md

File metadata and controls

36 lines (27 loc) · 695 Bytes

Notes

Remember, the input array is sorted.

Solution

class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int i = 0;
        for (int n : nums) {
            if (i == 0 || nums[i - 1] != n) {
                nums[i] = n;
                i++;
            }
        }
        return i;
    }
}

Time/Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Similar Problems

Remove Duplicates from Sorted Array II

Links