Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.
Return k after placing the final result in the first k slots of nums.
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
Input: nums = [1,1,1]
Output: 1, nums = [1,_,_]
Explanation: Your function should return k = 1, with the first element of nums being 1. It does not matter what you leave beyond the returned k (hence they are underscores).
nums is sorted in non-decreasing order.Since the array is sorted, all duplicates appear consecutively. We can use a two-pointer technique where one pointer tracks the position of the last unique element, and the other scans the array to find the next distinct value.
j starting at index 0 (first element is always unique).i from index 1 onward.nums[i] differs from nums[j], increment j and copy nums[i] to nums[j].j+1 elements are unique. Return j+1 as the count of unique elements.class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.empty()) return 0;
int j = 0;
for (int i = 1; i < nums.size(); i++) {
if (nums[i] != nums[j]) {
j++;
nums[j] = nums[i];
}
}
return j + 1;
}
};
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int j = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] != nums[j]) {
j++;
nums[j] = nums[i];
}
}
return j + 1;
}
}
class Solution:
def removeDuplicates(self, nums):
if not nums:
return 0
j = 0
for i in range(1, len(nums)):
if nums[i] != nums[j]:
j += 1
nums[j] = nums[i]
return j + 1
class Solution {
removeDuplicates(nums) {
if (nums.length === 0) return 0;
let j = 0;
for (let i = 1; i < nums.length; i++) {
if (nums[i] !== nums[j]) {
j++;
nums[j] = nums[i];
}
}
return j + 1;
}
}
Time Complexity: O(n), where n is the number of elements in the array. Each element is inspected exactly once by the read pointer.
Space Complexity: O(1), since we modify the array in-place using only two extra pointers regardless of input size.
Your notes are automatically saved in your browser's local storage and will persist across sessions on this device.