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. Then return the number of unique elements in nums.
Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:
nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.k.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).
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).
1 <= nums.length <= 3 * 10^4-10^4 <= nums[i] <= 10^4nums is sorted in non-decreasing order.In this problem, the key point to focus on is the input array being sorted. As far as duplicate elements are concerned, what is their positioning in the array when the given array is sorted? Look at the image below for the answer. If we know the position of one of the elements, do we also know the positioning of all the duplicate elements?
We need to modify the array in-place and the size of the final array would potentially be smaller than the size of the input array. So, we ought to use a two-pointer approach here. One, that would keep track of the current element in the original array and another one for just the unique elements.
Essentially, once an element is encountered, you simply need to bypass its duplicates and move on to the next unique element.
No developer has claimed the Python throne for LeetCode #26 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
The numbers in the array are already sorted, so any duplicate values must appear consecutively.
To remove duplicates, we need to keep every number that is different from the previous one, and discard the rest.
Since the array needs to be modified in place, two pointers are required: one moves fast and the other moves slow.
the fast pointer increases its position by 1 each time, while the slow pointer marks the current position to be modified.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
slow = 1
# nums = [0,0,1,1,1,2,2,3,3,4]
for fast in range(1, len(nums)):
if nums[fast] == nums[slow - 1]:
continue
nums[slow] = nums[fast]
slow += 1
return slow
No developer has claimed the Ruby throne for LeetCode #26 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
The numbers in the array are already sorted, so any duplicate values must appear consecutively.
To remove duplicates, we need to keep every number that is different from the previous one, and discard the rest.
Since the array needs to be modified in place, two pointers are required: one moves fast and the other moves slow.
the fast pointer increases its position by 1 each time, while the slow pointer marks the current position to be modified.
# @param {Integer[]} nums
# @return {Integer}
def remove_duplicates(nums)
slow = 1
# nums = [0,0,1,1,1,2,2,3,3,4]
(1...nums.size).each do |fast|
if nums[fast] == nums[slow - 1]
next
end
nums[slow] = nums[fast]
slow += 1
end
slow
end
No developer has claimed the Java throne for LeetCode #26 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
The numbers in the array are already sorted, so any duplicate values must appear consecutively.
To remove duplicates, we need to keep every number that is different from the previous one, and discard the rest.
Since the array needs to be modified in place, two pointers are required: one moves fast and the other moves slow.
the fast pointer increases its position by 1 each time, while the slow pointer marks the current position to be modified.
class Solution {
public int removeDuplicates(int[] nums) {
var slow = 1;
// nums = [0,0,1,1,1,2,2,3,3,4]
for (var fast = 1; fast < nums.length; fast++) {
if (nums[fast] != nums[slow - 1]) {
nums[slow] = nums[fast];
slow++;
}
}
return slow;
}
}
No developer has claimed the C++ throne for LeetCode #26 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Javascript throne for LeetCode #26 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Go throne for LeetCode #26 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Rust throne for LeetCode #26 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the C# throne for LeetCode #26 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #26 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #26 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.