File: remove-element/solution.py

Date: 2026-06-06

Time: 18:46

remove-element/solution.py

Purpose

This file implements LeetCode 27 — Remove Element. It solves the in-place array mutation variant where you must remove all occurrences of a given value from an array and return the count of remaining elements. The solution modifies nums directly — the caller inspects only the first k elements after the call.

Key Components

removeElement(nums, val) -> int — The sole function. It takes a mutable list and a value to remove, compacts the non-matching elements to the front of the list, and returns the count k of retained elements. After the call, nums[0:k] contains exactly the elements not equal to val, in their original relative order.

Patterns

Two-pointer compaction (read/write pointers). This is the canonical in-place partition pattern:

The write pointer only advances when an element is retained, so it always satisfies k <= i. Elements at indices >= k are "garbage" — they may contain stale values, and the problem contract says they're ignored.

This is the same pattern used in remove-duplicates-from-sorted-array/solution.py and move-zeroes/solution.py in this repo. It's the standard approach when the problem requires stable in-place filtering with O(1) extra space.

Dependencies

Imports: None. Pure function with no external dependencies.

Imported by: The remove-element/test_solution.py file directly. The "Imported By" list in the prompt shows ~400+ test files — this is an artifact of how the test harness is structured (likely a shared conftest or test utility that re-exports solution modules), not direct usage of removeElement itself.

Flow

1. Initialize write pointer k = 0.

2. Iterate read pointer i from 0 to len(nums) - 1.

3. If nums[i] != val, copy nums[i] to nums[k] and increment k.

4. If nums[i] == val, skip — k doesn't advance, so this element is effectively dropped.

5. Return k.

For input nums = [3, 2, 2, 3], val = 3:

Invariants

Error Handling

None. The function assumes valid inputs per the LeetCode contract: nums is a list of integers, val is an integer. Empty lists work correctly — the loop body never executes and k=0 is returned.