PKM Note: Array Duplicate Detection via Expected Arithmetic Sum

  • Topic: Algorithms & Data Structures / Array Searching

  • Tags: #dsa #algorithms #arrays #math #problem-solving

  • Related Notes: [[Floyd-Cycle-Detection]], [[XOR-Duplicate-Detection]], [[Prefix-Sums]]

1. Problem Definition

Given an array AA of size LL containing:

  1. Every integer in the continuous range [0,M][0, M] at least once (where M=max⁑(A)M = \max(A)).

  2. Exactly one repeating integer RR that appears k+1k + 1 times (kβ‰₯1k \ge 1 extra occurrences).

Find the value of the repeating element RR in O(N)\mathcal{O}(N) time and O(1)\mathcal{O}(1) auxiliary space.

2. Core Mathematical Intuition

The sum of an arithmetic series from 00 to MM is deterministic:

Sexpected=βˆ‘i=0Mi=M(M+1)2S_{\text{expected}} = \sum_{i=0}^{M} i = \frac{M(M+1)}{2}

Because every integer from 00 to MM is guaranteed to appear exactly once, plus kk surplus copies of RR, the actual sum of array elements is:

Sactual=Sexpected+(kβ‹…R)S_{\text{actual}} = S_{\text{expected}} + (k \cdot R)

Isolating the repeating value RR:

R=Sactualβˆ’SexpectedkR = \frac{S_{\text{actual}} - S_{\text{expected}}}{k}

Where:

  • M=max⁑(A)M = \max(A)

  • k=Lβˆ’(M+1)k = L - (M + 1) (total elements minus unique element count)

3. Algorithm & Complexity

Steps

  1. Traverse array once to compute:

    • Sactual=βˆ‘A[i]S_{\text{actual}} = \sum A[i]

    • M=max⁑(A)M = \max(A)

  2. Compute Sexpected=M(M+1)2S_{\text{expected}} = \frac{M(M+1)}{2}.

  3. Compute k=length(A)βˆ’(M+1)k = \text{length}(A) - (M + 1).

  4. Return Sactualβˆ’Sexpectedk\frac{S_{\text{actual}} - S_{\text{expected}}}{k}.

Complexity

  • Time Complexity: O(N)\mathcal{O}(N) β€” single pass over the array.

  • Space Complexity: O(1)\mathcal{O}(1) β€” constant auxiliary memory.

4. Implementation

Python

Python

def find_repeating_element(arr: list[int]) -> int:
    """Finds the single repeating element in an array containing [0..max(arr)].

    Constraints: All elements in [0..max(arr)] present, exactly one element
    repeats.
    """
    n = len(arr)
    max_val = 0
    actual_sum = 0

    for x in arr:
        actual_sum += x
        if x > max_val:
            max_val = x

    expected_sum = (max_val * (max_val + 1)) // 2
    extra_count = n - (max_val + 1)

    if extra_count <= 0:
        raise ValueError("No duplicate elements found in array.")

    return (actual_sum - expected_sum) // extra_count

C++ (Overflow-Safe)

C++

#include <vector>
#include <numeric>
#include <algorithm>
#include <stdexcept>

long long findRepeatingElement(const std::vector<int>& arr) {
    long long actual_sum = 0;
    long long max_val = 0;

    for (int x : arr) {
        actual_sum += x;
        if (x > max_val) {
            max_val = x;
        }
    }

    long long expected_sum = (max_val * (max_val + 1)) / 2;
    long long extra_count = static_cast<long long>(arr.size()) - (max_val + 1);

    if (extra_count <= 0) {
        throw std::invalid_argument("No duplicate elements found.");
    }

    return (actual_sum - expected_sum) / extra_count;
}

5. Edge Cases & Boundary Conditions

ScenarioInput ExampleExpected BehaviorFormula Handling
Repeating Zero[0, 0, 1, 2]R=0R = 0Sactualβˆ’Sexpected=0β€…β€ŠβŸΉβ€…β€Š01=0S_{\text{actual}} - S_{\text{expected}} = 0 \implies \frac{0}{1} = 0 (Correct)
Large Extra Count[0, 1, 2, 2, 2, 2]R=2,k=3R = 2, k = 310βˆ’33=73\frac{10 - 3}{3} = \frac{7}{3} (if M=2M=2, Sexp=3,Sact=9β€…β€ŠβŸΉβ€…β€Š63=2S_{\text{exp}}=3, S_{\text{act}}=9 \implies \frac{6}{3} = 2) (Correct)
Integer OverflowNβ‰₯105N \ge 10^5Sum exceeds 231βˆ’12^{31}-1Mitigate using 64-bit integer types (long long, int64_t).

6. Precondition Checklist & Failure Modes

This approach only holds when the strict invariant is respected:

[Is every integer in 0..max(arr) present?] ──No──> FAILS (Gaps invalidate S_expected)
                    β”‚
                   Yes
                    β–Ό
[Is only ONE distinct value duplicated?]  ──No──> FAILS (Returns weighted average of duplicates)
                    β”‚
                   Yes
                    β–Ό
          [Math Approach Valid]

Alternative Techniques for Relaxed Constraints:

  • Missing numbers / Gaps present: Use Hash Set (O(N)\mathcal{O}(N) space) or Floyd’s Cycle Detection / Index Negation (O(1)\mathcal{O}(1) space, if array can be mutated and range is [1..N][1..N]).

  • Multiple distinct duplicates: Use Frequency Map / Hash Table or Boolean Bitset.

Local Graph View

Start typing to search
Try: two sum or #Arrays or #Amazon