Java ProgramsArraysFind Missing Number

Find Missing Number in Java

intermediate·  Arrays  ·  Array

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.

Input
[1, 2, 4, 5, 6, 7], n = 7
Output
Missing number: 3

Java Program

Java
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

Missing number: 3

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.

How It Works
  1. 1n * (n + 1) / 2 computes the expected sum of every integer from 1 to n, using the standard arithmetic series formula.
  2. 2A loop adds up every element actually present in arr, building actualSum.
  3. 3expectedSum - actualSum is exactly the value that's missing from the array.
  4. 4No sorting or searching is needed — the missing number falls straight out of the arithmetic.
For 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.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

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

arithmetic sum formulaarray length

Approach 2: XOR Trick

Java
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

Missing number: 3

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.

How It Works
  1. 1result starts at 0 and is XOR-ed with every integer from 1 to n.
  2. 2result is then XOR-ed with every element actually present in arr.
  3. 3Every number that appears in both the full range and the array gets XOR-ed with itself, which cancels it out to 0.
  4. 4Only the missing number never gets canceled, so it's exactly what's left in result at the end.
For 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.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: Two single passes — one over the full range, one over the array — each XOR one value into a single running result.

Key Concepts

bitwise XORXOR self-cancellation

Approach 3: Java 8

Java
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

Missing number: 3

Core Logic

The same sum-formula idea can be expressed with streams — sum the full range, sum the array, and subtract.

How It Works
  1. 1IntStream.rangeClosed(1, n).sum() adds up every integer from 1 to n, replacing the arithmetic formula with an explicit sum.
  2. 2Arrays.stream(arr).sum() adds up every element actually present in the array.
  3. 3Subtracting the array's sum from the full range's sum leaves exactly the missing number, the same as the manual version.
For 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.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: Both streams reduce directly to a single sum without collecting anything, so only the two intermediate totals are held.

Key Concepts

StreamIntStream.rangeClosed()sum()

Related Programs