Remove Duplicates from Sorted List
Real-world version: Trim Repeat HoldsThe statement here is our own: the same inputs, outputs and technique.
A freeze-dryer runs a recipe built from holds wired one behind another. A hold knows only its target temperature in Celsius and which hold comes after it, and nothing in the machine records how many holds a recipe has. Targets never fall as the run proceeds, and neighbouring holds may carry the same target.
The recipe reaches you as holds, a flat list of the target temperatures in run order. That flat list is the JSON stand-in for the wired chain, so [] stands for a recipe with no holds in it at all, and the answer is a flat list of the same kind.
A hold that repeats the target of the hold in front of it changes nothing about the run, so unhook every such hold and hand back the chain that remains, in run order. Each target that appears in the recipe has to appear exactly once in the answer. Unhook the repeats from the chain you were handed instead of assembling a fresh chain alongside it.
Examples
Example 1
- Input
- holds = [-40, -40, -12, 5, 5, 5]
- Output
- [-40, -12, 5]
The second -40 repeats the hold in front of it, and two of the three 5 holds do the same, so those three unhook and the chain keeps -40, -12 and 5 in run order.
Example 2
- Input
- holds = [8, 8]
- Output
- [8]
The second hold repeats the first, so it unhooks and one hold at 8 is left.
Example 3
- Input
- holds = [-100, -7, 0, 63]
- Output
- [-100, -7, 0, 63]
No hold repeats the target in front of it, so the chain comes back untouched.
Example 4
- Input
- holds = []
- Output
- []
A recipe with no holds has nothing to unhook, so the answer is an empty chain.
Constraints
- 0 <= holds.length <= 300
- -100 <= holds[i] <= 100
- holds is given in non-decreasing order
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.
def trim_repeat_holds(holds: list[int]) -> list[int]:public int[] trimRepeatHolds(int[] holds)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 16 test cases: 16 passed
- Passed: 4 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's