Remove Element: keep the survivors up front
Strip every copy of a value out of an array in place and return k, the count of what's left — order doesn't matter, and anything past k is ignored. It's the purest write-pointer drill, with a neat variant for when removals are rare.
A write pointer packs the keepers
Read every element with i; keep a second pointer k for where the next survivor belongs. Whenever the value isn't the one we're removing, copy it down to k and bump k. Skips cost nothing.
k only advances when you keep something — so it doubles as the running count. At the end it is the answer. One pass, no extra array.Swap from the end instead
The slow-fast pass copies every survivor — wasteful if you're only removing a handful. This variant touches the array only when it finds a val: it grabs a replacement from the far end and shrinks the range.
2 is rare. L scans from the left, R guards the end. Instead of copying every keeper, we only act on the bad ones.Reach for the write pointer
Unless the interviewer stresses that removals are rare, the slow-fast version is the one to write — it's the cleanest, and it generalizes everywhere.
i, write with k. Copy nums[i] to nums[k] and bump k only when it isn't val; skip otherwise. k ends as both the new length and the count. O(n) time, O(1) space."