Show problems as
Classic problem #2150MediumArrayHash TableCounting

Find All Lonely Numbers in the Array

Real-world version: Isolated Dock Numbers
Tracked in this browser only
Read Find All Lonely Numbers in the Array on LeetCode(opens in a new tab)

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

A bike-share audit lists the dock every bike was returned to yesterday. The list docks holds one dock number per return, so a dock number appears once for each bike returned to it.

Docks are numbered so that dock d - 1 and dock d + 1 are the two docks standing either side of dock d. Call a dock number isolated when it appears exactly once in docks and neither d - 1 nor d + 1 appears in docks at all.

Return every isolated dock number, in ascending order. Return an empty list when there is none.

Examples

Example 1

Input
docks = [42, 7, 19, 20]
Output
[7, 42]

Dock 7 was used once and docks 6 and 8 are absent, and dock 42 was used once with 41 and 43 absent. Docks 19 and 20 stand beside each other and both appear, so neither qualifies.

Example 2

Input
docks = [5, 6, 10, 10, 3]
Output
[3]

Dock 3 was used once with 2 and 4 absent. Dock 10 was used twice, and docks 5 and 6 sit next to each other.

Example 3

Input
docks = [42, 42]
Output
[]

Dock 42 appears twice, so it is not isolated and nothing is reported.

Constraints

  • 1 <= docks.length <= 10^5
  • 0 <= docks[i] <= 10^6

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 isolated_docks(docks: list[int]) -> list[int]:
Java
public List<Integer> isolatedDocks(int[] docks)