Show problems as
Classic problem #83EasyLinked List

Remove Duplicates from Sorted List

Real-world version: Trim Repeat Holds
Tracked in this browser only
Read Remove Duplicates from Sorted List on LeetCode(opens in a new tab)

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

Learn the pattern firstDummy Node Technique · Linked List, animated step by step

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.

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