LeetCode solutions

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

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 123ms, memory 41.4MB, accepted 2025-12-29.

python
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

Source