Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an integer array nums with the following properties:
nums.length == 2 * n.nums contains n + 1 unique values, n of which occur exactly once in the array.nums is repeated n times.Return the element that is repeated n times.
Example 1:
Input: nums = [1,2,3,3] Output: 3
Example 2:
Input: nums = [2,1,2,5,3,2] Output: 2
Example 3:
Input: nums = [5,1,5,2,5,3,5,4] Output: 5
Constraints:
2 <= n <= 5000nums.length == 2 * n0 <= nums[i] <= 104nums contains n + 1 unique elements and one of them is repeated exactly n times.Problem summary: You are given an integer array nums with the following properties: nums.length == 2 * n. nums contains n + 1 unique values, n of which occur exactly once in the array. Exactly one element of nums is repeated n times. Return the element that is repeated n times.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,3,3]
[2,1,2,5,3,2]
[5,1,5,2,5,3,5,4]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #961: N-Repeated Element in Size 2N Array
class Solution {
public int repeatedNTimes(int[] nums) {
Set<Integer> s = new HashSet<>(nums.length / 2 + 1);
for (int i = 0;; ++i) {
if (!s.add(nums[i])) {
return nums[i];
}
}
}
}
// Accepted solution for LeetCode #961: N-Repeated Element in Size 2N Array
func repeatedNTimes(nums []int) int {
s := map[int]bool{}
for i := 0; ; i++ {
if s[nums[i]] {
return nums[i]
}
s[nums[i]] = true
}
}
# Accepted solution for LeetCode #961: N-Repeated Element in Size 2N Array
class Solution:
def repeatedNTimes(self, nums: List[int]) -> int:
s = set()
for x in nums:
if x in s:
return x
s.add(x)
// Accepted solution for LeetCode #961: N-Repeated Element in Size 2N Array
struct Solution;
use std::collections::HashSet;
impl Solution {
fn repeated_n_times(a: Vec<i32>) -> i32 {
let mut hs: HashSet<i32> = HashSet::new();
for x in a {
if !hs.insert(x) {
return x;
}
}
unreachable!()
}
}
#[test]
fn test() {
let a = vec![1, 2, 3, 3];
assert_eq!(Solution::repeated_n_times(a), 3);
let a = vec![2, 1, 2, 5, 3, 2];
assert_eq!(Solution::repeated_n_times(a), 2);
let a = vec![5, 1, 5, 2, 5, 3, 5, 4];
assert_eq!(Solution::repeated_n_times(a), 5);
}
// Accepted solution for LeetCode #961: N-Repeated Element in Size 2N Array
function repeatedNTimes(nums: number[]): number {
const s: Set<number> = new Set();
for (const x of nums) {
if (s.has(x)) {
return x;
}
s.add(x);
}
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.