Ctrl K

Drop the Unwanted Value

Easy
You are given an integer array nums and a number unwanted. Remove every occurrence of unwanted from the array, working directly in nums without a second array. Say k elements are left afterwards. The first k cells of nums must then hold exactly those elements, in any order; whatever sits in the cells past them does not matter. Return k.
Example 1
nums=[4, 1, 4, 4, 9], unwanted=4 → 2

Once every 4 is gone, 1 and 9 remain, which is two elements. The first two cells of nums must hold 1 and 9 in any order.

Example 2
nums=[3, 3], unwanted=3 → 0

Every element equals unwanted, so nothing is left.

Example 3
nums=[0, 2, 5], unwanted=7 → 3

There is no 7 in the array, so all three elements stay.

Constraints
  • 0 ≤ nums.length ≤ 100
  • 0 ≤ nums[i] ≤ 50
  • 0 ≤ unwanted ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSqueeze Repeats Out of a Sorted Array EasyMerge Into the Spare Tail EasyZeros to the Back EasyValues in Both Lists EasyShared Values With Repeats Easy
loading Python…
Input
nums=[4, 1, 4, 4, 9], unwanted=4
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 111 tests on the server, hidden ones included.