Remove Element
Remove every copy of a value in place and return how many values are left, using a write pointer.
01The problem
Remove every occurrence of val from nums in place. Return k, the number of values left; the first k slots must hold them (in any order). What's after slot k − 1 doesn't matter.
| Input | Output | First k slots |
|---|---|---|
nums = [3, 2, 2, 3], val = 3 | 2 | [2, 2] |
nums = [0,1,2,2,3,0,4,2], val = 2 | 5 | [0, 1, 3, 0, 4] |
Loading the rest of the lesson…