Find All Lonely Numbers in the Array - Problem
Find All Lonely Numbers in the Array

You are given an integer array nums. A number x is considered lonely when it satisfies two conditions:

1. It appears exactly once in the array
2. Neither of its adjacent numbers (x - 1 and x + 1) exist in the array

For example, if x = 5 appears once, it's lonely only if neither 4 nor 6 appear anywhere in the array.

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

Example: In array [10, 6, 5, 8], number 10 is lonely (appears once, no 9 or 11), but 5 is not lonely (appears once but 6 exists).

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [10, 6, 5, 8]
โ€บ Output: [10, 8]
๐Ÿ’ก Note: Number 10 appears once and neither 9 nor 11 exist. Number 8 appears once and neither 7 nor 9 exist. Number 6 appears once but 5 exists (adjacent). Number 5 appears once but 6 exists (adjacent).
example_2.py โ€” No Lonely Numbers
$ Input: nums = [1, 3, 5, 3]
โ€บ Output: []
๐Ÿ’ก Note: Number 1 appears once but no neighbors to check - however, since we have consecutive gaps, let's reconsider. Actually, 1 appears once and neither 0 nor 2 exist, so 1 should be lonely. Number 3 appears twice (not lonely). Number 5 appears once and neither 4 nor 6 exist, so 5 should be lonely. Correct output should be [1, 5].
example_3.py โ€” Single Element
$ Input: nums = [7]
โ€บ Output: [7]
๐Ÿ’ก Note: Number 7 appears once and neither 6 nor 8 exist in the array, making it lonely.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 0 โ‰ค nums[i] โ‰ค 106
  • Array may contain duplicate numbers
  • Important: A number must appear exactly once AND have no adjacent numbers to be lonely

Visualization

Tap to expand
Finding Lonely Numbers VisualizationInput Array: [10, 6, 5, 8]10658Blue = Lonely, Red = Not LonelyAnalysis Process:Checking Each NumberNumber 10:โœ“ Appears 1 timeโœ“ No 9 in arrayโœ“ No 11 in arrayโ†’ 10 is LONELY!Number 6:โœ“ Appears 1 timeโœ“ No 5 in array... wait!โœ— Found 5 in arrayโ†’ 6 is NOT lonelyNumber 5:โœ“ Appears 1 timeโœ“ No 4 in arrayโœ— Found 6 in arrayโ†’ 5 is NOT lonelyNumber 8:โœ“ Appears 1 timeโœ“ No 7 in arrayโœ“ No 9 in arrayโ†’ 8 is LONELY!Final Answer[10, 8]Why this works:โ€ข Hash map gives O(1) lookup timeโ€ข Check frequency AND neighborsโ€ข Overall O(n) time complexityNumbers 5 and 6 are neighbors, so neither can be lonely despite appearing only once
Understanding the Visualization
1
Count Attendance
Build a guest list showing how many times each person appeared
2
Find Loners
Identify guests who came exactly once
3
Check Friends
For each loner, see if their close friends (ยฑ1) attended
4
Identify Antisocial
Guests who came alone AND whose friends didn't show are truly lonely
Key Takeaway
๐ŸŽฏ Key Insight: Use a hash map to efficiently check both frequency (must be 1) and neighbor existence (xยฑ1 must not exist). The optimal solution processes each unique number only once.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
47.2K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen