2270. Find All Lonely Numbers in the Array
My accepted Python solution to LeetCode problem 2270, Find All Lonely Numbers in the Array, running in 123ms.
- Difficulty: Medium
- Python
- Runtime 123ms
- Memory 41.4MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 123ms, memory 41.4MB, accepted 2025-12-29.
class Solution:
def findLonely(self, nums: List[int]) -> List[int]:
from collections import Counter
count = Counter(nums)
result = []
for num in nums:
if count[num] == 1 and count.get(num - 1, 0) == 0 and count.get(num + 1, 0) == 0:
result.append(num)
return result