Remove All Occurrences of a Value From an Array In Place

Quick Overview

Remove every occurrence of a given value from an integer array in place and return how many elements remain, with the kept values packed at the front of the array in any order. The problem tests the read and write two-pointer pattern and safe in-place array updates.

Remove All Occurrences of a Value From an Array In Place

Company: Shield Ai

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer array `nums` and an integer `val`, remove all occurrences of `val` from `nums` in place. The order of the kept elements may be changed. Then return the number of elements of `nums` that are not equal to `val`. Let `k` be the number of elements not equal to `val`. When the function returns, the first `k` positions of `nums` must hold exactly those elements. Whatever is left in `nums` from index `k` onward does not matter. ### Function Signature ```python def remove_element(nums: list[int], val: int) -> int: ... ``` ### Rules - Modify `nums` in place and return `k`. The checker reads the returned `k` and then `nums[:k]`. - The kept elements may appear in `nums[:k]` in any order. The checker compares `sorted(nums[:k])` with the expected values sorted ascending, so every valid arrangement passes. - Positions from `k` to the end of `nums` are not checked and may hold anything. ### Constraints - `0 <= len(nums) <= 100` - `0 <= nums[i] <= 50` - `0 <= val <= 100` ### Examples ```text Input: nums = [3,2,2,3], val = 3 Output: 2, with sorted(nums[:2]) = [2,2] ``` Both 3s are removed. The two 2s remain, so `k = 2` and the first two positions hold `[2,2]`. ```text Input: nums = [0,1,2,2,3,0,4,2], val = 2 Output: 5, with sorted(nums[:5]) = [0,0,1,3,4] ``` The three 2s are removed and five values remain: 0, 1, 3, 0 and 4. Any arrangement of them in `nums[:5]` is accepted, such as `[0,1,3,0,4]` or `[0,1,4,0,3]`. ```text Input: nums = [4,4,4], val = 4 Output: 0, with sorted(nums[:0]) = [] ``` Every element equals `val`, so nothing is kept and `k = 0`.

Overview: Remove every occurrence of a given value from an integer array in place and return how many elements remain, with the kept values packed at the front of the array in any order. The problem tests the read and write two-pointer pattern and safe in-place array updates.

Shield Ai logo
Shield Ai
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums and an integer val, remove all occurrences of val from nums in place. The order of the kept elements may be changed. Then return the number of elements of nums that are not equal to val.

Let k be the number of elements not equal to val. When the function returns, the first k positions of nums must hold exactly those elements. Whatever is left in nums from index k onward does not matter.

Function Signature

def remove_element(nums: list[int], val: int) -> int:
    ...

Rules

  • Modify nums in place and return k . The checker reads the returned k and then nums[:k] .
  • The kept elements may appear in nums[:k] in any order. The checker compares sorted(nums[:k]) with the expected values sorted ascending, so every valid arrangement passes.
  • Positions from k to the end of nums are not checked and may hold anything.

Constraints

  • 0 <= len(nums) <= 100
  • 0 <= nums[i] <= 50
  • 0 <= val <= 100

Examples

Input: nums = [3,2,2,3], val = 3
Output: 2, with sorted(nums[:2]) = [2,2]

Both 3s are removed. The two 2s remain, so k = 2 and the first two positions hold [2,2].

Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, with sorted(nums[:5]) = [0,0,1,3,4]

The three 2s are removed and five values remain: 0, 1, 3, 0 and 4. Any arrangement of them in nums[:5] is accepted, such as [0,1,3,0,4] or [0,1,4,0,3].

Input: nums = [4,4,4], val = 4
Output: 0, with sorted(nums[:0]) = []

Every element equals val, so nothing is kept and k = 0.