This looks like a deletion problem, but arrays in most languages can't shrink in place. So the real task is to rearrange the array so that all elements not equal to val sit at the front, then return how many there are. Whatever sits past that prefix is ignored.
One detail drives the choice of algorithm: the problem allows the order of the remaining elements to change. That permission lets us avoid shifting elements we are about to discard. A left-to-right scan with a write pointer works without using that permission, and a two-pointer swap exploits it to do fewer writes.
0 <= nums.length <= 100 → The array can be empty, so the code must return 0 without indexing into it.0 <= nums[i] <= 50 and 0 <= val <= 100 → Since val ranges up to 100 while elements stay within 0 to 50, val may not appear in the array at all. In that case every element is kept and the answer equals the array length.Create a new array, copy over everything that isn't val, then write those kept elements back into the front of nums. This uses extra space rather than editing in place, but it separates the two concerns (selecting elements and placing them) into independent steps, which makes a useful baseline before optimizing.
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 works but allocates a second array. The next approach writes kept elements directly into the front of nums, removing the extra array entirely.
A single write pointer k tracks where the next kept element belongs. A read index i scans the array. Every time nums[i] is not equal to val, we copy it to nums[k] and advance k. Values equal to val are read past without advancing k, so they get overwritten by later kept elements.
The write pointer k is always at or behind the read index i, because k advances only when i advances and i advances on every iteration. So writing nums[k] = nums[i] either overwrites a position already read or writes i onto itself when k == i. No element we still need to read is overwritten before it is read.
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.This approach copies every kept element, even when it already sits in the right place. When val is rare, that is one write per element for almost the whole array. The next approach uses the order-doesn't-matter permission to write only when it finds a val to remove.
Approach 2 preserves order even though the problem allows reordering, so it writes more often than needed when val is rare. This approach uses two pointers: i from the front and n marking the current end. When nums[i] equals val, we overwrite position i with the element at nums[n-1] and decrement n, discarding the val in one write. After this move, i does not advance, because the element pulled in from the end has not been checked yet and could itself equal val. When nums[i] is not val, we keep it and advance i.
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.