Find All Lonely Numbers in the Array

You are given an integer array nums. A number x is lonely when it appears only once, and no adjacent numbers (i.e. x + 1 and x - 1) appear in the array.

Return all lonely numbers in nums. You may return the answer in any order.

Example 1
Inputnums = [10,6,5,8]
Output[10,8]
10 and 8 each appear exactly once and neither adjacent value appears in nums, while 5 and 6 are adjacent to each other.
Example 2
Inputnums = [1,3,5,3]
Output[1,5]
1 and 5 each appear exactly once and neither adjacent value appears in nums, while 3 appears twice.

Constraints

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

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate