Show problems as
Classic problem #27EasyArrayTwo Pointers

Remove Element

Real-world version: Discard Fault Samples
Tracked in this browser only
Read Remove Element on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A soil probe writes one reading per minute into a fixed buffer samples. One reading value, fault, is a diagnostic code rather than a measurement, so every occurrence of it has to be squeezed out of the buffer.

Rewrite samples in place so that its first kept slots hold exactly the readings that differ from fault, keeping those readings in the order they were recorded. Slots from index kept onward may hold anything; nobody reads them. You may not allocate a second buffer that grows with the input, so the rewrite has to use O(1) extra space.

Because the grader can only inspect a return value, return the pair [kept, samples[:kept]]: the number of surviving readings followed by the surviving prefix itself. The prefix must be in recorded order, so the answer is unique.

Examples

Example 1

Input
samples = [4, 9, 4, 7, 4], fault = 4
Output
[2, [9, 7]]

Three diagnostic codes drop out; the two real readings slide left and keep their recorded order.

Example 2

Input
samples = [21, 8, 21, 21, 13, 8], fault = 21
Output
[3, [8, 13, 8]]

The buffer starts with a code and holds a run of two more in the middle, so the three survivors compact into the first three slots.

Example 3

Input
samples = [12], fault = 40
Output
[1, [12]]

No reading matches the code, so nothing moves and the whole buffer survives.

Constraints

  • 0 <= samples.length <= 100
  • 0 <= samples[i] <= 50
  • 0 <= fault <= 100

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def discard_fault_samples(samples: list[int], fault: int) -> list:
Java
public int[] discardFaultSamples(int[] samples, int fault)