Values Appearing Alone Without Consecutive Neighbors

Quick Overview

Values Appearing Alone Without Consecutive Neighbors. A value x qualifies when it appears exactly once in nums and neither x-1 nor x+1 appears anywhere. Return all qualifying values in any order. Numerical neighbors matter, not adjacency of array positions.

Values Appearing Alone Without Consecutive Neighbors

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem A value `x` is lonely if it occurs exactly once in `nums` and neither `x - 1` nor `x + 1` occurs anywhere in the array. Return every lonely value, in any order. These are neighboring integer values, not neighboring array positions. ```cpp class Solution { public: vector<int> findLonely(vector<int>& nums); }; ``` ### Examples **Example 1:** ```text nums = [0,2,2,5] Output: [0,5] Explanation: Each occurs once without a numerical neighbor; 2 is repeated. ``` **Example 2:** ```text nums = [4,5,8] Output: [8] Explanation: 4 and 5 disqualify each other, while 8 stands alone. ``` ### Constraints - `1 <= nums.length <= 10^5`. - `0 <= nums[i] <= 10^6`. ### Clarifying Questions Repeated values never qualify. No order is imposed on the result. ```hint Inspect the structure What information is lost by using only a set of present values? ``` ### What a Strong Answer Covers - Require exactly one occurrence and check both neighboring integer values. - Return every qualifying value once while allowing any output order. - Handle zero, repeated values and separated array positions containing consecutive values. ### Follow-up Questions How could already sorted input support the same checks?

Overview: Values Appearing Alone Without Consecutive Neighbors. A value x qualifies when it appears exactly once in nums and neither x-1 nor x+1 appears anywhere. Return all qualifying values in any order. Numerical neighbors matter, not adjacency of array positions.

Apple logo
Apple
May 22, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

A value x is lonely if it occurs exactly once in nums and neither x - 1 nor x + 1 occurs anywhere in the array. Return every lonely value, in any order. These are neighboring integer values, not neighboring array positions.

class Solution {
public:
    vector<int> findLonely(vector<int>& nums);
};

Examples

Example 1:

nums = [0,2,2,5]
Output: [0,5]
Explanation: Each occurs once without a numerical neighbor; 2 is repeated.

Example 2:

nums = [4,5,8]
Output: [8]
Explanation: 4 and 5 disqualify each other, while 8 stands alone.

Constraints

  • 1 <= nums.length <= 10^5 .
  • 0 <= nums[i] <= 10^6 .

Clarifying Questions Guidance

Repeated values never qualify. No order is imposed on the result.

What a Strong Answer Covers Guidance

  • Require exactly one occurrence and check both neighboring integer values.
  • Return every qualifying value once while allowing any output order.
  • Handle zero, repeated values and separated array positions containing consecutive values.

Follow-up Questions Guidance

How could already sorted input support the same checks?