Array
Missing Number
Algorithm
Data Structures
Problem Solving

Quickest way to find missing number in an array of numbers

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Finding the missing number in an array is a common problem that can be encountered in technical interviews and algorithm exercises. The quickest and most efficient solution often depends on the specific nature of the array and constraints. This article delves into various methods to solve this problem, focusing on the most rapid approach, supported with technical explanations and practical examples.

Introduction

In a typical scenario, you may have an array with a sequence of numbers, say from 1 to `n`, with one number missing. The challenge is to identify that missing number efficiently. Several methods get the job done, ranging from simple iteration to more mathematically intuitive approaches.

Method 1: Using the Sum Formula

The most efficient way, generally speaking, is to use the formula for the sum of an arithmetic series. The sum of numbers from 1 to `n` is given by the formula:

S=n(n+1)2S = \frac{n(n + 1)}{2}

Given an array that goes from 1 to `n`, but missing one number, the process is simple:

  1. Calculate the expected sum using the formula.
  2. Find the actual sum of the array.
  3. The difference between the expected sum and the actual sum gives the missing number.

Example

Consider the array `[1, 2, 4, 5, 6]`.

  1. Calculate expected sum for `n=6`:
    S=6(6+1)2=21S = \frac{6 \cdot (6 + 1)}{2} = 21
  2. Calculate actual sum:
    Sactual=1+2+4+5+6=18S_{actual} = 1 + 2 + 4 + 5 + 6 = 18
  3. The missing number:
    Missing Number=2118=3Missing\ Number = 21 - 18 = 3

This method is both quick and doesn't require extra space, making it an O(n)O(n) time complexity solution with O(1)O(1) space complexity.

Method 2: Using XOR Operation

A secondary approach involves XOR (exclusive OR) operations. Leveraging XOR properties can achieve this with similar time complexity and no additional space.

The XOR operation has a few useful properties:

  1. `a ⊕ a = 0` for any integer `a`.
  2. `a ⊕ 0 = a` for any integer `a`.
  3. XOR is commutative and associative.

The approach:

  1. XOR all numbers from 1 to `n`.
  2. XOR all numbers in the array.
  3. XOR the results from steps 1 and 2. The missing number is obtained by the properties described above.

Example

For the array `[1, 2, 4, 5, 6]` and `n=6`:

  1. XOR from 1 to 6:
    `1 ⊕ 2 ⊕ 3 ⊕ 4 ⊕ 5 ⊕ 6 = 7`
  2. XOR the array:
    `1 ⊕ 2 ⊕ 4 ⊕ 5 ⊕ 6 = 2`
  3. XOR of above results:
    `7 ⊕ 2 = 3`

This method again features O(n)O(n) time complexity and O(1)O(1) space complexity.

Comparison of Methods

MethodTime ComplexitySpace ComplexityProsCons
Sum FormulaO(n)O(n)O(1)O(1)Simple math operation, clear logicMay face integer overflow for large n
XOR OperationO(n)O(n)O(1)O(1)Avoids overflow issues, clever use of XORMay be less intuitive to understand for beginners

Conclusion

Both methods offered are efficient, with their own advantages and constraints. The sum formula method is straightforward but can face integer overflow issues with large numbers. The XOR method is a clever bit-manipulation hack that bypasses math constraints but may be less immediately intuitive. Choosing the right one depends on the problem constraints and personal comfort with the underlying techniques.

In practice, either method will typically perform exceedingly well in relevant problem scenarios. Familiarity with both can prove advantageous for tackling an array of related algorithmic challenges.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.