026.Remove Duplicates from Sorted Array

Test cases

1
2
3
4
5
[]
[0, 2]
[2, 2]
[1, 1, 2]
[0, 1, 2]

Solution 1: accepted 13ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public int removeDuplicates(int[] nums) {
int len = nums.length;
if (nums.length < 2)
return len;
int count = 1;
for (int i = 1; i < len; i++) {
if (nums[i - 1] != nums[i]) {
nums[count] = nums[i];
count++;
}
}
return count;
}