Last Updated: March 31, 2026
At first, this seems like you just need to delete elements from an array. But arrays in most languages don't support true deletion, you can't shrink them. So the real task is to rearrange the array so that all elements not equal to val are packed at the front, and then return how many there are.
The key detail: the problem says the order of remaining elements may be changed. This is more relaxed than Move Zeroes (which requires preserving relative order), and that relaxation opens up an interesting optimization. But even if we ignore it, a simple left-to-right scan with a write pointer works perfectly.
0 <= nums.length <= 100 → With n at most 100, even O(n^2) would be trivially fast. This is an Easy problem where the focus is on technique, not performance.0 <= nums[i] <= 50 → All values are non-negative and small. No tricky edge cases with negative numbers or large values.0 <= val <= 100 → The value to remove might not even exist in the array (val could be 51-100 while all elements are 0-50).The simplest way to think about this: create a new array, copy over everything that isn't val, then copy it all back. This violates the spirit of "in-place," but it makes the logic dead simple and is a good starting point for understanding the problem.
result.nums. For each element not equal to val, add it to result.result back into the first positions of nums.result.Input:
After the first pass (collect non-val elements into result):
Finally, we copy result back into nums:
This approach works correctly but uses extra space.
What if we wrote non-val elements directly into their final positions within the original array?
Here's the key insight: we can use a single pointer k to track where the next "kept" element should be placed. As we scan through the array with index i, every time nums[i] is not equal to val, we write it at position k and advance the pointer.
The pointer k is always at or behind i. So when we write nums[k] = nums[i], we're either overwriting a position we've already processed or overwriting the same position (when k == i). We never lose data we still need.
The relative order of non-val elements is preserved because we scan left-to-right and write left-to-right.
k = 0 to track the write position.i from 0 to n - 1.nums[i] != val, write nums[i] to nums[k] and increment k.k is the count of non-val elements, and nums[0..k-1] contains them all.The front overwrite approach copies every kept element, even when it's already in position. Since the problem says order doesn't matter, can we do fewer writes by swapping with the end instead?
The problem says the order of remaining elements doesn't matter. Approach 2 preserves order anyway, which means it does more work than necessary when val is rare. The trick: use two pointers, one at the start and one at the end. When nums[i] equals val, swap it with the last element and shrink the effective array.
i = 0 and n = nums.length (n tracks the effective end of the array).i < n:nums[i] == val, copy nums[n - 1] to nums[i] and decrement n. Don't advance i because the swapped-in element hasn't been checked yet.nums[i] != val, advance i.n.i or decrements n, so at most n iterations total.