27. Remove Element

Practice Remove Element for free on NudgeCode: Easy, Array & Hash Table pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

Remove every occurrence of val from nums in place and return k, the number of elements that are not equal to val.

After your function returns, the first k elements of nums must be exactly the elements that are not val, in any order. Elements after the first k can be anything. Modify nums itself instead of building a new array.

The test checks both k and the first k elements of nums, written as [k, nums[:k]]. The order of those k elements doesn't matter.

Example

nums = [0,1,2,2,3,0,4,2], val = 2  ->  [5, [0,1,3,0,4]]

The function returns 5; [0,1,4,0,3] or any other order of the same five values also passes.

Constraints