Find All Numbers Disappeared in an Array

Easy

Topics
ArrayHash Table

Given an array nums of n integers where nums[i] is in the range [1, n], return an array (ascending) of all integers in [1, n] that do not appear in nums.

Example 1

nums43278231
missing56
Values are confined to 1..8, so each value has a home index. Missing: 5, 6.
Top row is the input; bottom row is the values that never appear.
Input:  nums = [4,3,2,7,8,2,3,1]
Output: [5,6]

Example 2

nums11
missing2
Missing: 2.
Input:  nums = [1,1]
Output: [2]

Constraints

  • n == nums.length
  • 1 <= n <= 10^5
  • 1 <= nums[i] <= n
Editorial — intuition, approach, complexity and pitfalls

Intuition

The array holds n values drawn from 1..n, with some repeated, and we want the values from that range that never appear. A hash set of what is present, then a scan of 1..n, answers it directly in O(n) time and O(n) space — which is the solution shown.

The reason this problem is interesting is its follow-up: O(1) extra space, without destroying the input's information. The unlock is the same as in several problems with this shape — the value range and the index range are the same size, so the array can encode its own membership.

Walk the array and, for each value v, mark position v-1 as "seen" by making the number there negative. Negation is reversible and leaves the magnitude intact, so a later read can still recover the original value with abs. Afterwards, any position still holding a positive number was never marked, so index + 1 is missing.

💡 Sign as a marker bit: the values are all positive by constraint, so the sign bit is unused storage sitting inside the array.

Approach

  1. Build a set from the input.
  2. Return every x in 1..n absent from the set.

For the O(1) variant: negate nums[abs(v)-1] for each v, then collect i+1 for every index still positive.

Dry Run

nums = [4,3,2,7,8,2,3,1]   n = 8
seen  = {1,2,3,4,7,8}
scan 1..8:  1✓ 2✓ 3✓ 4✓ 5✗ 6✗ 7✓ 8✓
answer [5,6]

sign-marking variant, same input:
  after marking, indices 4 and 5 remain positive
  -> 5 and 6 missing          (same answer, no set)

Complexity

  • Time — O(n): two linear passes.
  • Space — O(n): the set, as written. The output list is the result rather than scratch space, so the sign-marking variant is O(1) auxiliary — which is what the follow-up asks for, and the only reason to prefer it.

Common Pitfalls

  • Forgetting abs when indexing in the sign-marking variant. By the time you read a value it may already be negative, and a negative index silently reads from the wrong end.
  • Negating twice for a duplicate, flipping it back to positive. Set the sign rather than toggling it: negate only if currently positive, or use -abs(...).
  • Confusing the two offsets. Values are 1..n and indices 0..n-1, so value v marks index v-1 and index i reports value i+1.
  • Sorting first. It works and is O(n log n), but throws away the property that makes O(n) possible.

Full Solution — all languages

class Solution:
    def findDisappearedNumbers(self, nums):
        seen = set(nums)
        return [x for x in range(1, len(nums) + 1) if x not in seen]
SensAI
Beta
Tap the mic to stop voice mode