File: n-repeated-element-in-size-2n-array/solution.py

Date: 2026-06-06

Time: 18:11

n-repeated-element-in-size-2n-array/solution.py

Purpose

This file solves LeetCode 961: N-Repeated Element in Size 2N Array. It owns a single function that identifies the repeated element in an array with a specific structure: length 2n, containing n+1 unique values where exactly one value appears n times.

Key Components

repeatedntimes(nums: list[int]) -> int — The sole public function. Takes a 2n-length array and returns the element that appears n times. The contract guarantees exactly one such element exists per the problem constraints.

Patterns

Early-return with set membership — The solution uses a hash set to detect the first duplicate encountered during a linear scan. The moment num in seen is true, that element must be the n-repeated one, because all other elements appear exactly once. This is a standard duplicate-detection idiom.

The function returns mid-iteration rather than scanning the entire array. Since the repeated element appears n times in a 2n-length array (i.e., half the elements), by the pigeonhole principle, a duplicate must be found within the first n+1 elements at most.

Dependencies

Imports: None — the solution uses only Python builtins (set).

Imported by: The "Imported By" list in the prompt is misleading — it lists hundreds of unrelated test files. The actual direct consumer is n-repeated-element-in-size-2n-array/testsolution.py, which imports repeatedn_times to verify correctness.

Flow

1. Initialize an empty set called seen.

2. Iterate through nums one element at a time.

3. For each element, check set membership (O(1) average).

4. If already seen, return it immediately — this is the answer.

5. Otherwise, add it to the set and continue.

No post-loop return exists. The function relies on the problem guarantee that a duplicate always exists, so the loop always terminates via the early return.

Invariants

Error Handling

None. The function trusts its input completely, consistent with the LeetCode convention where inputs are guaranteed valid. An empty list would cause the function to return None; a list with no duplicates would do the same.

Complexity

Topics to Explore

Beliefs