27. Remove Element
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 remove all occurrences of a specific value val from an integer array nums in-place. We need to return k, which is the count of elements remaining in the array. The key constraints are that the modification must happen in-place (i.e., with O(1) extra memory) and the relative order of the elements that are not equal to val can be changed. The elements in the array beyond the first k elements do not matter.
Solution
Remove Element in Python
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
# 'k' is the slow-runner pointer. It indicates the next position
# where an element not equal to 'val' should be placed.
k = 0
# 'i' is the fast-runner pointer. It iterates through the entire array.
for i in range(len(nums)):
# If the current element is not the value to be removed...
if nums[i] != val:
# ...we place it at the position 'k'.
# This overwrites any 'val' element that was there
# or an element that has already been moved.
nums[k] = nums[i]
# Increment 'k' to point to the next empty slot.
k += 1
# After the loop, 'k' is the number of elements that are not equal to 'val',
# which is the new logical length of the array.
return k
Remove Element in Java
class Solution {
public int removeElement(int[] nums, int val) {
// 'k' is the slow-runner pointer. It indicates the next position
// where an element not equal to 'val' should be placed.
int k = 0;
// 'i' is the fast-runner pointer. It iterates through the entire array.
for (int i = 0; i < nums.length; i++) {
// If the current element is not the value to be removed...
if (nums[i] != val) {
// ...we place it at the position 'k'.
// This overwrites any 'val' element that was there
// or an element that has already been moved.
nums[k] = nums[i];
// Increment 'k' to point to the next empty slot.
k++;
}
}
// After the loop, 'k' is the number of elements that are not equal to 'val',
// which is the new logical length of the array.
return k;
}
}
Remove Element in C++
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
// 'k' is the slow-runner pointer. It indicates the next position
// where an element not equal to 'val' should be placed.
int k = 0;
// 'i' is the fast-runner pointer. It iterates through the entire array.
for (int i = 0; i < nums.size(); i++) {
// If the current element is not the value to be removed...
if (nums[i] != val) {
// ...we place it at the position 'k'.
// This overwrites any 'val' element that was there
// or an element that has already been moved.
nums[k] = nums[i];
// Increment 'k' to point to the next empty slot.
k++;
}
}
// After the loop, 'k' is the number of elements that are not equal to 'val',
// which is the new logical length of the array.
return k;
}
};
Remove Element in TypeScript
function removeElement(nums: number[], val: number): number {
// 'k' is the slow-runner pointer. It indicates the next position
// where an element not equal to 'val' should be placed.
let k = 0;
// 'i' is the fast-runner pointer. It iterates through the entire array.
for (let i = 0; i < nums.length; i++) {
// If the current element is not the value to be removed...
if (nums[i] !== val) {
// ...we place it at the position 'k'.
// This overwrites any 'val' element that was there
// or an element that has already been moved.
nums[k] = nums[i];
// Increment 'k' to point to the next empty slot.
k++;
}
}
// After the loop, 'k' is the number of elements that are not equal to 'val',
// which is the new logical length of the array.
return k;
};
More Easy 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.