Watch 10 video solutions for Remove Element, a easy level problem involving Array, Two Pointers. This walkthrough by NeetCode has 159,351 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 all occurrences of val in-place and return the number of remaining elements. The array must be modified without allocating extra space, so the solution relies on careful index movement rather than creating a new array.
Approach 1: Two-Pointer (Overwrite) Technique (Time: O(n), Space: O(1))
This method scans the array once while maintaining a write index for valid elements. Iterate through the array using a read pointer. Every time you see a number that is not equal to val, copy it to the write pointer and move the write pointer forward. Elements equal to val are skipped, effectively overwriting them with valid elements encountered later. The key insight: the write pointer always marks the next position where a non-val element should go.
This preserves the relative order of remaining elements. Because every element is visited exactly once, the time complexity is O(n) and the algorithm uses constant extra memory O(1). This pattern frequently appears in array filtering problems and is a classic application of the two pointers technique.
Approach 2: Two-Pointer with Swap from End (Time: O(n), Space: O(1))
This variation avoids unnecessary writes when many elements equal val. Use two pointers: one starting from the beginning and another from the end of the array. When the left pointer encounters val, swap it with the element at the right pointer and decrease the right pointer. If the element at the left pointer is valid, simply move forward.
The insight here is that order does not matter for this problem. Swapping with the end quickly pushes unwanted values out of the active region. Each swap shrinks the valid search space from the right side, which keeps the algorithm linear with O(n) time and constant O(1) space. This version can perform fewer writes when many elements must be removed.
Recommended for interviews: The overwrite two-pointer solution is the most commonly expected answer. It demonstrates clear understanding of in-place array manipulation and pointer movement. The swap-based method is a useful optimization when order is irrelevant and shows deeper knowledge of two-pointer tradeoffs. Mentioning both approaches during interviews signals strong problem-solving depth.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Two-Pointer (Overwrite) | O(n) | O(1) | General case when you want a simple linear scan and to preserve relative order of elements |
| Two-Pointer with Swap from End | O(n) | O(1) | When element order does not matter and many values must be removed, reducing unnecessary writes |