2150. Find All Lonely Numbers in the Array LeetCode Solution
In this guide, you will get 2150. Find All Lonely Numbers in the Array LeetCode Solution with the best time and space complexity. The solution to Find All Lonely Numbers in the Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find All Lonely Numbers in the Array solution in C++
- Find All Lonely Numbers in the Array solution in Java
- Find All Lonely Numbers in the Array solution in Python
- Additional Resources
Problem Statement of 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:
Input: nums = [10,6,5,8]
Output: [10,8]
Explanation:
– 10 is a lonely number since it appears exactly once and 9 and 11 does not appear in nums.
– 8 is a lonely number since it appears exactly once and 7 and 9 does not appear in nums.
– 5 is not a lonely number since 6 appears in nums and vice versa.
Hence, the lonely numbers in nums are [10, 8].
Note that [8, 10] may also be returned.
Example 2:
Input: nums = [1,3,5,3]
Output: [1,5]
Explanation:
– 1 is a lonely number since it appears exactly once and 0 and 2 does not appear in nums.
– 5 is a lonely number since it appears exactly once and 4 and 6 does not appear in nums.
– 3 is not a lonely number since it appears twice.
Hence, the lonely numbers in nums are [1, 5].
Note that [5, 1] may also be returned.
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 106
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2150. Find All Lonely Numbers in the Array LeetCode Solution in C++
class Solution {
public:
vector<int> findLonely(vector<int>& nums) {
vector<int> ans;
unordered_map<int, int> count;
for (const int num : nums)
++count[num];
for (const auto& [num, freq] : count)
if (freq == 1 && !count.contains(num - 1) && !count.contains(num + 1))
ans.push_back(num);
return ans;
}
};
/* code provided by PROGIEZ */
2150. Find All Lonely Numbers in the Array LeetCode Solution in Java
class Solution {
public List<Integer> findLonely(int[] nums) {
List<Integer> ans = new ArrayList<>();
Map<Integer, Integer> count = new HashMap<>();
for (final int num : nums)
count.merge(num, 1, Integer::sum);
for (final int num : count.keySet())
if (count.get(num) == 1 && !count.containsKey(num - 1) && !count.containsKey(num + 1))
ans.add(num);
return ans;
}
}
// code provided by PROGIEZ
2150. Find All Lonely Numbers in the Array LeetCode Solution in Python
class Solution:
def findLonely(self, nums: list[int]) -> list[int]:
count = collections.Counter(nums)
return [num for num, freq in count.items()
if freq == 1 and
count[num - 1] == 0 and
count[num + 1] == 0]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.