LeetCode Solutions
26. Remove Duplicates from Sorted Array
Time: $O(n)$ Space: $O(1)$
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int i = 0;
for (const int num : nums)
if (i < 1 || num > nums[i - 1])
nums[i++] = num;
return i;
}
};
class Solution {
public int removeDuplicates(int[] nums) {
int i = 0;
for (final int num : nums)
if (i < 1 || num > nums[i - 1])
nums[i++] = num;
return i;
}
}
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
i = 0
for num in nums:
if i < 1 or num > nums[i - 1]:
nums[i] = num
i += 1
return i