Watch 10 video solutions for Remove Element, a easy level problem involving Array, Two Pointers. This walkthrough by NeetCode has 122,167 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.
Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:
nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.k.Custom Judge:
The judge will test your solution with the following code:
int[] nums = [...]; // Input array
int val = ...; // Value to remove
int[] expectedNums = [...]; // The expected answer with correct length.
// It is sorted with no values equaling val.
int k = removeElement(nums, val); // Calls your implementation
assert k == expectedNums.length;
sort(nums, 0, k); // Sort the first k elements of nums
for (int i = 0; i < actualLength; i++) {
assert nums[i] == expectedNums[i];
}
If all assertions pass, then your solution will be accepted.
Example 1:
Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2,_,_] Explanation: Your function should return k = 2, with the first two elements of nums being 2. It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2:
Input: nums = [0,1,2,2,3,0,4,2], val = 2 Output: 5, nums = [0,1,4,0,3,_,_,_] Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4. Note that the five elements can be returned in any order. It does not matter what you leave beyond the returned k (hence they are underscores).
Constraints:
0 <= nums.length <= 1000 <= nums[i] <= 500 <= val <= 100Problem Overview: You are given an integer array nums and a value val. Remove every occurrence of val in-place and return the new length of the array. The order of elements can change, and extra elements beyond the returned length do not matter.
Approach 1: Two-pointer Overwrite (O(n) time, O(1) space)
This method scans the array once while maintaining a write pointer. Iterate through the array using a read pointer. Every time you encounter a value that is not equal to val, write it to the current write index and increment the write pointer. Elements equal to val are skipped, effectively removing them. Since the overwrite happens inside the same array, the algorithm runs in O(n) time and uses O(1) extra space.
The key insight: treat the array like a stream of valid elements and compact them toward the front. This pattern appears frequently in two pointers problems where you filter or compress values while scanning.
Approach 2: Two-pointer with Swap from End (O(n) time, O(1) space)
This approach maintains two pointers: one starting at the beginning and another at the end of the array. When the left pointer encounters val, swap it with the element at the end pointer and decrease the end pointer. Do not move the left pointer immediately, because the swapped element still needs to be checked. If the current value is not val, simply advance the left pointer.
This technique works well when you are allowed to change the element order. Instead of shifting values forward, unwanted elements are replaced by elements from the back. Each element is processed at most once, giving O(n) time complexity and constant O(1) space.
The strategy is common in array manipulation tasks where removing elements without preserving order simplifies the logic. It avoids repeated writes to the front portion of the array.
Recommended for interviews: The overwrite two-pointer approach is the most commonly expected answer. It demonstrates understanding of in-place array filtering using two pointers. The swap-from-end method is also valid and sometimes more efficient when many elements equal val, but interviewers typically prefer the forward compaction pattern because it keeps the remaining elements in order and is easier to reason about.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Two-pointer Overwrite | O(n) | O(1) | General case when filtering values while preserving relative order |
| Two-pointer with Swap from End | O(n) | O(1) | When element order does not matter and you want fewer writes to the front |