Ctrl K

Numbers Without Neighbours

Medium
You are given an integer array values. A number x from the array is isolated if it appears exactly once and neither x − 1 nor x + 1 appears in the array. Return all isolated numbers in any order.
Example 1
values=[4, 9, 3, 12, 9, 15] → [12, 15]

4 and 3 are neighbours and 9 repeats. 12 and 15 each appear once, with none of 11, 13, 14 or 16 present.

Example 2
values=[0] → [0]

A lone number is always isolated.

Example 3
values=[6, 7, 7] → []

6 has the neighbour 7, and 7 repeats.

Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 0 ≤ values[i] ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMore Than a Third of the Votes MediumMost Common Values MediumJobs With a Cooldown MediumMost Searched Words MediumDomain Visit Rollup Medium
loading Python…
Input
values=[4, 9, 3, 12, 9, 15]
Expected
[12, 15]

Run checks the first 3 tests in your browser. Submit checks all 147 tests on the server, hidden ones included.