Find the missing number in an array containing 0..n. How many ways can you solve it?
Problem
An array holds n distinct numbers taken from 0..n, so exactly one is missing. [3, 0, 1] → 2. One of the most frequently asked warm-up questions in Java interviews, because it has many correct solutions with different trade-offs.
Pattern
Hashing or math on the whole range. When the input is "almost" a known set, compare it to that set: with a set (memory), with a sum or XOR (constant memory), or by sorting.
Approach 1: Check every candidate (feasible, O(n²))
class MissingBrute {
static int find(int[] nums) {
for (int candidate = 0; candidate <= nums.length; candidate++) {
boolean found = false;
for (int x : nums) if (x == candidate) { found = true; break; }
if (!found) return candidate;
}
throw new IllegalArgumentException("nothing missing");
}
}Approach 2: Sort, then find the first gap (O(n log n))
import java.util.Arrays;
class MissingSort {
static int find(int[] nums) {
int[] a = nums.clone();
Arrays.sort(a);
for (int i = 0; i < a.length; i++) if (a[i] != i) return i;
return a.length; // 0..n-1 all present, so n is missing
}
}Approach 3: HashSet (O(n) time, O(n) space)
import java.util.HashSet;
import java.util.Set;
class MissingHashSet {
static int find(int[] nums) {
Set<Integer> seen = new HashSet<>();
for (int x : nums) seen.add(x);
for (int i = 0; i <= nums.length; i++) if (!seen.contains(i)) return i;
throw new IllegalArgumentException("nothing missing");
}
}Approach 4: Java 8 streams with the sum formula (O(n), O(1))
Expected sum of 0..n is n(n+1)/2; the difference is the missing number. Use long to avoid overflow for large n.
import java.util.Arrays;
class MissingStreams {
static int find(int[] nums) {
long n = nums.length;
long expected = n * (n + 1) / 2;
long actual = Arrays.stream(nums).asLongStream().sum();
return (int) (expected - actual);
}
}Approach 5: XOR (O(n), O(1), no overflow at all) — optimal
x ^ x = 0 and x ^ 0 = x, so XOR-ing every index and every value cancels all pairs and leaves the missing number.
class MissingXor {
static int find(int[] nums) {
int xor = nums.length;
for (int i = 0; i < nums.length; i++) xor ^= i ^ nums[i];
return xor;
}
}
class MissingDemo {
public static void main(String[] args) {
int[] nums = {9, 6, 4, 2, 3, 5, 7, 0, 1};
System.out.println(MissingBrute.find(nums) + " " + MissingSort.find(nums) + " " + MissingHashSet.find(nums)
+ " " + MissingStreams.find(nums) + " " + MissingXor.find(nums)); // 8 8 8 8 8
}
}Feasible → optimal
- Check every candidate: O(n²).
- Sort: O(n log n), and it mutates or copies the array.
HashSet: O(n) time but O(n) extra memory and boxing.- Sum formula (loop or
IntStream.sum()): O(n), O(1); watch overflow, uselong. - XOR: O(n), O(1), overflow-proof: the best answer to give.
Follow-up questions interviewers ask
Try answering each one before you open it.
Related (same pattern): find the duplicate and the missing number ("set mismatch").
With sums: let d = sum(nums) - sum(1..n) (duplicate minus missing) and q = sumOfSquares(nums) - sumOfSquares(1..n) (dup² − missing²); then dup + missing = q / d, which gives both. With a boolean array or sign-marking (negate nums[|x|-1]; seeing an already negative value reveals the duplicate) it is O(n) and O(1) extra space.
Related (same pattern): find the single number when every other number appears twice.
XOR everything: pairs cancel, the single number remains. O(n), O(1). Variant "every other number appears three times": count bits modulo 3 for each of the 32 bit positions, or use the ones/twos bitmask trick.
Related (same pattern): find all numbers missing from 1..n when duplicates are allowed.
Mark presence in place: for each value v, negate nums[|v| - 1] if positive; afterwards every index i still holding a positive value means i + 1 is missing. O(n) time, O(1) extra space (excluding the output). With streams: IntStream.rangeClosed(1, n).filter(i -> !set.contains(i)) using a HashSet.