Find All Numbers Disappeared in an Array
Easy
Topics
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
1..8, so each value has a home index. Missing: 5, 6.Input: nums = [4,3,2,7,8,2,3,1] Output: [5,6]
Example 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
- Build a set from the input.
- Return every
xin1..nabsent 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
abswhen 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..nand indices0..n-1, so valuevmarks indexv-1and indexireports valuei+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]import java.util.*;
class Solution {
public int[] findDisappearedNumbers(int[] nums) {
int n = nums.length;
boolean[] seen = new boolean[n + 1];
for (int x : nums) seen[x] = true;
List<Integer> res = new ArrayList<>();
for (int x = 1; x <= n; x++) if (!seen[x]) res.add(x);
int[] r = new int[res.size()];
for (int i = 0; i < r.length; i++) r[i] = res.get(i);
return r;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: vector<int> findDisappearedNumbers(vector<int>& nums) {
int n = (int)nums.size();
vector<bool> seen(n + 1, false);
for (int x : nums) seen[x] = true;
vector<int> r;
for (int x = 1; x <= n; x++) if (!seen[x]) r.push_back(x);
return r;
}
};/**
* @param {number[]} nums
* @return {number[]}
*/ var findDisappearedNumbers = function(nums) {
const n = nums.length, seen = new Array(n + 1).fill(false);
for (const x of nums) seen[x] = true;
const r = [];
for (let x = 1; x <= n; x++) if (!seen[x]) r.push(x);
return r;
};