Find Missing Number in Java
Problem
An array is missing exactly one number from a full run of 1 to n when it holds n - 1 of the n values that run should contain.
Given an array holding n - 1 distinct integers from 1 to n, find the one number missing from it.
Java Program
public class FindMissingNumber {
public static void main(String[] args) {
int[] arr = {1, 2, 4, 5, 6, 7};
int n = 7;
int expectedSum = n * (n + 1) / 2; // sum of every integer 1..n
int actualSum = 0;
for (int num : arr) {
actualSum += num; // sum of what's actually present
}
System.out.println("Missing number: " + (expectedSum - actualSum));
}
}Output
Core Logic
The sum of every number from 1 to n is a fixed, known value — subtracting the array's actual sum from that expected total leaves exactly the missing number.
- 1
n * (n + 1) / 2computes the expected sum of every integer from1ton, using the standard arithmetic series formula. - 2A loop adds up every element actually present in
arr, buildingactualSum. - 3
expectedSum - actualSumis exactly the value that's missing from the array. - 4No sorting or searching is needed — the missing number falls straight out of the arithmetic.
n = 7, the expected sum is 28; the array [1, 2, 4, 5, 6, 7] actually sums to 25, so the missing number is 28 - 25 = 3.Key Point: This only works when exactly one number is missing and every other value from 1 to n appears exactly once — with duplicates or multiple gaps, the arithmetic no longer isolates a single answer.
Why: Computing the expected sum is a constant-time formula, and the actual sum only needs a single pass through the array with one running total.
Key Concepts
Approach 2: XOR Trick
public class FindMissingNumberXor {
public static void main(String[] args) {
int[] arr = {1, 2, 4, 5, 6, 7};
int n = 7;
int result = 0;
// XOR every number in the full range 1..n
for (int i = 1; i <= n; i++) {
result ^= i;
}
// XOR every number actually in the array — matching values cancel out
for (int num : arr) {
result ^= num;
}
System.out.println("Missing number: " + result);
}
}
Output
Core Logic
XOR-ing every number from 1 to n together with every number actually in the array cancels out every value that appears in both, leaving only the missing one.
- 1
resultstarts at0and is XOR-ed with every integer from1ton. - 2
resultis then XOR-ed with every element actually present inarr. - 3Every number that appears in both the full range and the array gets XOR-ed with itself, which cancels it out to
0. - 4Only the missing number never gets canceled, so it's exactly what's left in
resultat the end.
n = 7 and [1, 2, 4, 5, 6, 7], every value except 3 is XOR-ed twice (once from the full range, once from the array) and cancels out, leaving result = 3.Key Point: Unlike the sum formula, this never risks integer overflow on very large arrays, since XOR never produces a value larger than the inputs involved.
Why: Two single passes — one over the full range, one over the array — each XOR one value into a single running result.
Key Concepts
Approach 3: Java 8
import java.util.Arrays;
import java.util.stream.IntStream;
public class FindMissingNumberStream {
public static void main(String[] args) {
int[] arr = {1, 2, 4, 5, 6, 7};
int n = 7;
// Sums the full range and the array, then takes the difference
int missing = IntStream.rangeClosed(1, n).sum() - Arrays.stream(arr).sum();
System.out.println("Missing number: " + missing);
}
}
Output
Core Logic
The same sum-formula idea can be expressed with streams — sum the full range, sum the array, and subtract.
- 1
IntStream.rangeClosed(1, n).sum()adds up every integer from1ton, replacing the arithmetic formula with an explicit sum. - 2
Arrays.stream(arr).sum()adds up every element actually present in the array. - 3Subtracting the array's sum from the full range's sum leaves exactly the missing number, the same as the manual version.
n = 7, IntStream.rangeClosed(1, 7).sum() gives 28, and Arrays.stream(arr).sum() gives 25, so 28 - 25 = 3.Key Point: rangeClosed(1, n).sum() still does the same amount of work as the closed-form formula — it just expresses 'sum every number 1 to n' literally instead of via the arithmetic shortcut.
Why: Both streams reduce directly to a single sum without collecting anything, so only the two intermediate totals are held.