Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 1050 <= nums[i] <= 106Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[10,6,5,8]
[1,3,5,3]
frequency-of-the-most-frequent-element)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2150: Find All Lonely Numbers in the Array
class Solution {
public List<Integer> findLonely(int[] nums) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : nums) {
cnt.merge(x, 1, Integer::sum);
}
List<Integer> ans = new ArrayList<>();
for (var e : cnt.entrySet()) {
int x = e.getKey(), v = e.getValue();
if (v == 1 && !cnt.containsKey(x - 1) && !cnt.containsKey(x + 1)) {
ans.add(x);
}
}
return ans;
}
}
// Accepted solution for LeetCode #2150: Find All Lonely Numbers in the Array
func findLonely(nums []int) (ans []int) {
cnt := map[int]int{}
for _, x := range nums {
cnt[x]++
}
for x, v := range cnt {
if v == 1 && cnt[x-1] == 0 && cnt[x+1] == 0 {
ans = append(ans, x)
}
}
return
}
# Accepted solution for LeetCode #2150: Find All Lonely Numbers in the Array
class Solution:
def findLonely(self, nums: List[int]) -> List[int]:
cnt = Counter(nums)
return [
x for x, v in cnt.items() if v == 1 and cnt[x - 1] == 0 and cnt[x + 1] == 0
]
// Accepted solution for LeetCode #2150: Find All Lonely Numbers in the Array
/**
* [2150] 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 <= 10^5
* 0 <= nums[i] <= 10^6
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/
// discuss: https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn find_lonely(nums: Vec<i32>) -> Vec<i32> {
vec![]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2150_example_1() {
let nums = vec![10, 6, 5, 8];
let result = vec![10, 8];
assert_eq!(Solution::find_lonely(nums), result);
}
#[test]
#[ignore]
fn test_2150_example_2() {
let nums = vec![1, 3, 5, 3];
let result = vec![1, 5];
assert_eq!(Solution::find_lonely(nums), result);
}
}
// Accepted solution for LeetCode #2150: Find All Lonely Numbers in the Array
function findLonely(nums: number[]): number[] {
const cnt: Map<number, number> = new Map();
for (const x of nums) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
const ans: number[] = [];
for (const [x, v] of cnt) {
if (v === 1 && !cnt.has(x - 1) && !cnt.has(x + 1)) {
ans.push(x);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.