"""
Testcases:
Input:
[]
[1,1,2]
[1,1,1]
[0]
Output:
[]
[1,2]
[1]
[0]
"""
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0;
res = 0; # res is used to maintain the n th element in nums without repetition
# res starts from 0;
for i in range(1,len(nums)):
if nums[i] != nums[res]:
res += 1;
nums[res] = nums[i];
return res + 1;
public class Solution {
public int removeDuplicates(int[] nums) {
int m = nums.length;
int res = 0;
for(int i = 1; i < m; i++){
if(nums[i] != nums[res]){
nums[++res] = nums[i];
}
}
return res + 1;
}
}