All problems
0206EasyLinked List

Trim Repeat Holds

Tracked in this browser only
Write code

Trains the technique from

LeetCode 83Remove Duplicates from Sorted List

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def trim_repeat_holds(holds: list[int]) -> list[int]:
Java
public int[] trimRepeatHolds(int[] holds)
September 7
Apply