Skip to content
Decoder

Remove Element

Easy

Problem

Given a list nums and a value val, remove every occurrence of `val` in place. Return k, the number of elements that are not val. The first `k` slots of nums must hold those elements, in any order; whatever comes after them does not matter.

Example 1

Input
nums = [3, 2, 2, 3], val = 3
Output
k = 2, first two are [2, 2]

Example 2

Input
nums = [0, 1, 2, 2, 3, 0, 4, 2], val = 2
Output
k = 5, first five are 0, 1, 3, 0, 4 in some order

Constraints

  • 0 <= nums.length <= 100
  • 0 <= nums[i] <= 50
  • 0 <= val <= 100
  • Use O(1) extra space.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.