AlgoMaster Logo

Find All Numbers Disappeared in an Array

Last Updated: March 29, 2026

easy

Understanding the Problem

We have an array of n integers, and every value falls in the range [1, n]. Some numbers in that range appear once, some appear more than once (as duplicates), and some don't appear at all. Our job is to find the ones that are missing.

The constraint that values are in [1, n] is the crucial detail. It means there's a natural one-to-one mapping between array indices (0 to n-1) and the values we expect (1 to n). If every number appeared exactly once, we'd have a permutation of [1, n]. The duplicates are the reason some numbers go missing. So the real question is: how can we efficiently figure out which numbers from [1, n] never show up?

Key Constraints:

  • 1 <= n <= 10^5 → With n up to 100,000, O(n^2) would mean 10 billion operations, which is too slow. We need O(n) or O(n log n) at most.
  • 1 <= nums[i] <= n → Every value maps naturally to an index in the array (value - 1). This is a strong hint that we can use the array itself as a tracking structure.
  • Follow-up asks for O(n) time and O(1) extra space → This rules out using a hash set or boolean array. We need to mark visited numbers using the input array itself.

Approach 1: Hash Set

Intuition

The most natural approach: throw all the numbers into a set, then check which numbers from 1 to n are missing. A set gives us O(1) lookups, so the whole process is straightforward.

This doesn't satisfy the follow-up's space requirement, but it's the clearest way to think about the problem and a good starting point in an interview.

Algorithm

  1. Insert all elements of nums into a hash set.
  2. Iterate through numbers from 1 to n.
  3. For each number, check if it exists in the set.
  4. If it doesn't, add it to the result list.
  5. Return the result list.

Example Walkthrough

Input:

0
4
1
3
2
2
3
7
4
8
5
2
6
3
7
1
nums

Build the set: {1, 2, 3, 4, 7, 8}. Then check each number from 1 to 8:

0
5
1
6
result

Code

This approach works correctly but uses O(n) extra space. Since values are in [1, n], each value points to a valid index. What if we used the input array itself as our bookkeeping structure?

Approach 2: Negation Marking (Optimal)

Intuition

Since every value in the array is between 1 and n, each value naturally maps to an index: the value v maps to index v - 1. If we could "mark" each index that gets pointed to, then any unmarked index tells us its corresponding number is missing.

But how do you mark an index without extra space? By negating the value at that position. All values start positive (they're in [1, n]), so a negative value at index i means the number i + 1 exists somewhere in the array. After one pass of marking, a second pass finds all indices still holding positive values. Those are our missing numbers.

Algorithm

  1. First pass (marking): For each element, compute the index it maps to: index = |nums[i]| - 1. If nums[index] is positive, negate it to mark that index + 1 is present.
  2. Second pass (collecting): Iterate through the array. For each index i where nums[i] is still positive, the number i + 1 is missing. Add it to the result.
  3. Return the result list.

Example Walkthrough

1Initial: nums = [4, 3, 2, 7, 8, 2, 3, 1]
0
4
i
1
3
2
2
3
7
4
8
5
2
6
3
7
1
1/9

Code

The negation approach modifies the input array. What if instead of marking, we physically sorted each number into its home position?

Approach 3: Cyclic Sort

Intuition

Since every value is in [1, n], each number has a "home" position: the number v belongs at index v - 1. If we could place every number in its home, then a simple scan would reveal which positions are occupied by the wrong number, and those wrong positions correspond to missing numbers.

This is the cyclic sort pattern. Instead of marking, we physically move each number to where it belongs. After sorting, index i should hold the value i + 1. Any index where this isn't true tells us i + 1 is missing.

Algorithm

  1. Iterate through the array. For each position i, repeatedly swap nums[i] into its correct position (nums[i] - 1) until the current element is already in the right place or the target position already holds the correct value (duplicate).
  2. After the cyclic sort, iterate through the array again. For each index i, if nums[i] != i + 1, then i + 1 is a missing number.
  3. Return the result list.

Example Walkthrough

1Initial: nums = [4, 3, 2, 7, 8, 2, 3, 1]
0
4
i
1
3
2
2
3
7
4
8
5
2
6
3
7
1
1/8

Code