Find every number from 1 to n that is missing from an array

Quick Overview

Given an array of n integers whose values all lie between 1 and n, return every value in that range that never appears. The problem tests index-based marking, handling duplicate values, and reaching linear time with constant extra space.

Find every number from 1 to n that is missing from an array

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array `nums` of `n` integers where every `nums[i]` is in the range `[1, n]`, return an array of all the integers in the range `[1, n]` that do not appear in `nums`. Values in `nums` may repeat. ### Function Signature ```python def find_disappeared_numbers(nums: list[int]) -> list[int]: ``` ### Rules - Return the missing values in ascending order, each value once. - If every value in `[1, n]` appears, return an empty list. ### Constraints - `n == len(nums)` - `1 <= n <= 10^5` - `1 <= nums[i] <= n` ### Examples ```text Input: nums = [4,3,2,7,8,2,3,1] Output: [5,6] ``` The array has length 8, so the range is 1 through 8. The values 1, 2, 3, 4, 7 and 8 appear, which leaves 5 and 6. ```text Input: nums = [1,1] Output: [2] ``` The range is 1 through 2. Only 1 appears, so 2 is missing. ```text Input: nums = [1] Output: [] ``` The single value 1 covers the whole range `[1, 1]`, so nothing is missing.

Overview: Given an array of n integers whose values all lie between 1 and n, return every value in that range that never appears. The problem tests index-based marking, handling duplicate values, and reaching linear time with constant extra space.

Apple logo
Apple
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array nums of n integers where every nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums. Values in nums may repeat.

Function Signature

def find_disappeared_numbers(nums: list[int]) -> list[int]:

Rules

  • Return the missing values in ascending order, each value once.
  • If every value in [1, n] appears, return an empty list.

Constraints

  • n == len(nums)
  • 1 <= n <= 10^5
  • 1 <= nums[i] <= n

Examples

Input: nums = [4,3,2,7,8,2,3,1]
Output: [5,6]

The array has length 8, so the range is 1 through 8. The values 1, 2, 3, 4, 7 and 8 appear, which leaves 5 and 6.

Input: nums = [1,1]
Output: [2]

The range is 1 through 2. Only 1 appears, so 2 is missing.

Input: nums = [1]
Output: []

The single value 1 covers the whole range [1, 1], so nothing is missing.