Find The Repeating And Missing Number

4 min read

Introduction

In many coding interviews and algorithmic challenges, you’ll encounter a classic problem: find the repeating and missing number in an array of size n that should contain each integer from 1 to n exactly once. Because one value appears twice (the repeating number) and another value never appears (the missing number), the task is to identify both with minimal extra space and time. This problem not only tests your understanding of array manipulation but also your ability to apply mathematical insights, bitwise operations, or hashing techniques efficiently. Mastering these approaches equips you with versatile tools for similar data‑validation tasks in real‑world software development Less friction, more output..

Problem Statement

You are given an array arr of length n. The array is supposed to hold every integer from 1 to n exactly once. Still, due to an error, one integer is duplicated (appears twice) and another integer is absent (missing) It's one of those things that adds up..

  1. The repeating number (the value that occurs twice).
  2. The missing number (the value that never occurs).

The constraints typically require an O(n) time complexity and O(1) auxiliary space, pushing you to think beyond brute‑force solutions Worth keeping that in mind..

Scientific Explanation

Underlying Mathematics

Let the array be arr[0…n‑1]. Define:

  • S = sum(arr) – the actual sum of elements.
  • S_expected = n·(n+1)/2 – the sum of numbers from 1 to n.
  • P = product(arr) – the actual product (conceptually).
  • P_expected = n! – the product of numbers from 1 to n.

If we subtract the expected sum from the actual sum, we get:

S - S_expected = (repeating - missing)

Similarly, using the difference of squares of sums:

S2 = sum of squares of arr
S2_expected = n·(n+1)·(2n+1)/6
S2 - S2_expected = (repeating² - missing²)

From these two equations we can solve for both numbers algebraically. Still, dealing with large products or squares can cause integer overflow, so most practical solutions avoid direct multiplication Less friction, more output..

Bitwise XOR Insight

The XOR operation has useful properties:

  • a ⊕ a = 0
  • a ⊕ 0 = a
  • XOR is commutative and associative.

If we XOR all array elements with numbers 1 through n, every number that appears exactly once cancels out, leaving:

xor_all = repeating ⊕ missing

Because repeating ≠ missing, xor_all is non‑zero. Pick any set bit (usually the rightmost set bit) and partition the numbers into two groups: those with that bit set and those without. XOR each group separately; each group will yield either the repeating or the missing number. A final pass identifies which is which by checking presence in the original array.

You'll probably want to bookmark this section.

Approaches

1. Mathematical Formula (Sum & Sum of Squares)

Steps

  1. Compute S = Σ arr[i] and S_expected = n·(n+1)/2.

  2. Compute diff = S - S_expected = repeating - missing.

  3. Compute S2 = Σ arr[i]² and S2_expected = n·(n+1)·(2n+1)/6 Worth keeping that in mind..

  4. Compute diff_squares = S2 - S2_expected = repeating² - missing² Most people skip this — try not to..

  5. Since repeating² - missing² = (repeating - missing)(repeating + missing), we have:

    repeating + missing = diff_squares / diff
    
  6. Solve the system:

    repeating = (diff + (repeating + missing)) / 2
    missing  = (repeating + missing - diff) / 2
    

Pros – No extra data structures, O(n) time.
Cons – Potential integer overflow for large n; careful with division.

2. XOR Method (Bitwise)

Steps

  1. Compute xor_all = 0. XOR each array element and also XOR numbers 1 through n.
  2. Identify a set bit in xor_all (e.g., rightmost = xor_all & -xor_all).
  3. Initialize x = 0, y = 0.
  4. For each arr[i] and each number j from 1 to n:
    • If arr[i] & rightmost is true, x ^= arr[i] else y ^= arr[i].
    • If j & rightmost is true, x ^= j else y ^= j.
  5. After the loops, x and y are the two distinct numbers (one repeating, one missing).
  6. Determine which is repeating by scanning the array once more: the one that appears is the repeating number; the other is missing.

Pros – No risk of overflow, O(n) time, O(1) space.
Cons – Slightly more code, requires careful bit manipulation.

3. Hashing / Frequency Array

Steps

  1. Create a boolean array seen[1…n] initialized to false.
  2. Iterate through arr:
    • If seen[arr[i]] is true, record arr[i] as repeating.
    • Else set seen[arr[i]] = true.
  3. After the pass, the index j where seen[j] is false is the missing number.

Pros – Straightforward, easy to understand.
Cons – Requires O(n) extra space, which may be undesirable for strict constraints.

4. Sorting Approach

Steps

  1. Sort arr (in‑place if allowed).
  2. Traverse the sorted array:
    • If arr[i] == arr[i+1], arr[i] is repeating.
    • If arr[i] + 1 != arr[i+1], arr[i] + 1 is missing.

Pros – Works without extra memory beyond sorting overhead.
Cons – Modifies the original array; time complexity O(n log n).

Example Walkthrough

Consider arr = [3, 1, 2, 5, 3]. Here n = 5.

  • Mathematical method:

    • S = 14, S_expected = 15, diff = -1 (repeating - missing = -1).
    • S2 = 3²+1²+2²+5²+3² = 9+1+4+25+9 = 48.
    • S2_expected = 55, diff_squares = -7.
    • repeating + missing = diff_squares / diff = (-7)/(-1) = 7.
    • Solve: repeating = (diff + sum)/2 = (-1+7)/2 = 3.
    • missing = sum - repeating = 7 - 3 = 4.
  • XOR method:

    • xor_all = 3⊕1⊕2⊕5⊕3⊕1⊕2⊕4⊕5 = 4.
    • Rightmost set bit = 4 (binary 100).
    • Partition yields x = 3, y = 4.
    • Scanning shows 3 appears
Just Dropped

Recently Added

In That Vein

Related Posts

Thank you for reading about Find The Repeating And Missing Number. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home