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 of size containing:
-
Every integer in the continuous range at least once (where ).
-
Exactly one repeating integer that appears times ( extra occurrences).
Find the value of the repeating element in time and auxiliary space.
2. Core Mathematical Intuition
The sum of an arithmetic series from to is deterministic:
Because every integer from to is guaranteed to appear exactly once, plus surplus copies of , the actual sum of array elements is:
Isolating the repeating value :
Where:
-
-
(total elements minus unique element count)
3. Algorithm & Complexity
Steps
-
Traverse array once to compute:
-
-
Compute .
-
Compute .
-
Return .
Complexity
-
Time Complexity: β single pass over the array.
-
Space Complexity: β 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
| Scenario | Input Example | Expected Behavior | Formula Handling |
|---|---|---|---|
| Repeating Zero | [0, 0, 1, 2] | (Correct) | |
| Large Extra Count | [0, 1, 2, 2, 2, 2] | (if , ) (Correct) | |
| Integer Overflow | Sum exceeds | Mitigate 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 ( space) or Floydβs Cycle Detection / Index Negation ( space, if array can be mutated and range is ).
-
Multiple distinct duplicates: Use Frequency Map / Hash Table or Boolean Bitset.