The Find All Lonely Numbers in the Array interview question introduces the concept of a "lonely" number. In a given integer array, a number is considered lonely if it appears exactly once in the array, and no adjacent numbers ( or ) exist anywhere in the array. Your task is to return all such lonely numbers in any order.
This problem is a standard assessment of basic data structure usage, specifically the Hash Table interview pattern. Companies like Apple and Google use it to see if a candidate can move beyond brute-force checks to a more efficient linear time complexity. It evaluations your ability to use frequency counting and set-based lookups to solve neighborhood-dependent constraints in a global context.
The most efficient pattern here is Frequency Counting using a Hash Table or a frequency map.
nums = [10, 6, 5, 8]
{10: 1, 6: 1, 5: 1, 8: 1}.[10, 10, 8] is given, 10 is not lonely even if 9 and 11 are missing, because it appears more than once.Whenever a problem involves checking for the existence of "neighbors" or "duplicates," think of a Hash Map or Hash Set. Linear time is almost always achievable if you can trade a small amount of space for lookup speed.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Longest Common Subsequence Between Sorted Arrays | Medium | Solve | |
| Count Pairs That Form a Complete Day II | Medium | Solve | |
| Tuple with Same Product | Medium | Solve | |
| Pairs of Songs With Total Durations Divisible by 60 | Medium | Solve | |
| Check If Array Pairs Are Divisible by k | Medium | Solve |