80. Remove Duplicates from Sorted Array II
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to modify a sorted array nums in-place to remove some duplicates. The rule is that each unique element can appear at most twice. We must preserve the relative order of the elements and return the new length k of the modified array prefix. The elements beyond k are irrelevant.
Solution
Remove Duplicates from Sorted Array II in Python
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
# k is the pointer for the next position to be filled.
# It also represents the length of the array with duplicates removed.
k = 0
# Iterate through each element of the array.
for num in nums:
# We should keep the current element `num` if:
# 1. The valid part of the array has fewer than 2 elements (k < 2).
# This is for the first two elements, which are always kept.
# 2. The current element `num` is greater than the element at `k - 2`.
# `nums[k-2]` is the second last element in our valid section.
# If `num` is greater than `nums[k-2]`, it means `num` is not a third
# consecutive duplicate. For example, if we have [1, 1, 1], when `num` is
# the third 1, `k` is 2. `nums[k-2]` which is `nums[0]` is 1. `1 > 1` is false.
if k < 2 or num > nums[k - 2]:
# If the condition is met, place the element at the k-th position
# and increment k.
nums[k] = num
k += 1
# k is the final length of the array.
return k
Remove Duplicates from Sorted Array II in Java
class Solution {
public int removeDuplicates(int[] nums) {
// k is the pointer for the next position to be filled.
// It also represents the length of the array with duplicates removed.
int k = 0;
// Iterate through each element of the array.
for (int num : nums) {
// We should keep the current element `num` if:
// 1. The valid part of the array has fewer than 2 elements (k < 2).
// This is for the first two elements, which are always kept.
// 2. The current element `num` is greater than the element at `k - 2`.
// `nums[k-2]` is the second last element in our valid section.
// If `num` is greater than `nums[k-2]`, it means `num` is not a third
// consecutive duplicate. For example, if we have [1, 1, 1], when `num` is
// the third 1, `k` is 2. `nums[k-2]` which is `nums[0]` is 1. `1 > 1` is false.
if (k < 2 || num > nums[k - 2]) {
// If the condition is met, place the element at the k-th position
// and increment k.
nums[k++] = num;
}
}
// k is the final length of the array.
return k;
}
}
Remove Duplicates from Sorted Array II in C++
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
// k is the pointer for the next position to be filled.
// It also represents the length of the array with duplicates removed.
int k = 0;
// Iterate through each element of the array.
for (int num : nums) {
// We should keep the current element `num` if:
// 1. The valid part of the array has fewer than 2 elements (k < 2).
// This is for the first two elements, which are always kept.
// 2. The current element `num` is greater than the element at `k - 2`.
// `nums[k-2]` is the second last element in our valid section.
// If `num` is greater than `nums[k-2]`, it means `num` is not a third
// consecutive duplicate. For example, if we have [1, 1, 1], when `num` is
// the third 1, `k` is 2. `nums[k-2]` which is `nums[0]` is 1. `1 > 1` is false.
if (k < 2 || num > nums[k - 2]) {
// If the condition is met, place the element at the k-th position
// and increment k.
nums[k++] = num;
}
}
// k is the final length of the array.
return k;
}
};
Remove Duplicates from Sorted Array II in TypeScript
function removeDuplicates(nums: number[]): number {
// k is the pointer for the next position to be filled.
// It also represents the length of the array with duplicates removed.
let k = 0;
// Iterate through each element of the array.
for (const num of nums) {
// We should keep the current element `num` if:
// 1. The valid part of the array has fewer than 2 elements (k < 2).
// This is for the first two elements, which are always kept.
// 2. The current element `num` is greater than the element at `k - 2`.
// `nums[k-2]` is the second last element in our valid section.
// If `num` is greater than `nums[k-2]`, it means `num` is not a third
// consecutive duplicate. For example, if we have [1, 1, 1], when `num` is
// the third 1, `k` is 2. `nums[k-2]` which is `nums[0]` is 1. `1 > 1` is false.
if (k < 2 || num > nums[k - 2]) {
// If the condition is met, place the element at the k-th position
// and increment k.
nums[k] = num;
k++;
}
}
// k is the final length of the array.
return k;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.