LeetCode Challenge Day 109 β 961. N-Repeated Element in Size 2N Array
Nitin Ahirwal / January 2, 2026
Hey folks π
This is Day 109 of my LeetCode streak π
Today's problem is 961. N-Repeated Element in Size 2N Array β a deceptively simple problem that rewards logical observation over brute force.
π Problem Statement
You are given:
- An integer array
numsof length2n - Exactly
n + 1unique elements - One element is repeated exactly
ntimes
Goal:
Return the element that is repeated n times.
π‘ Intuition
Since the array has length 2n and one element appears n times, that element is everywhere in the array.
This means:
- The repeated element cannot be spaced far apart everywhere
- In any small window of consecutive elements, the repeated element must appear at least twice
So instead of counting frequencies, we can detect repetition by simply comparing nearby elements.
π Approach
- Traverse the array from left to right
- For each index
i, comparenums[i]with:nums[i + 1]nums[i + 2]nums[i + 3]
- If any match is found, return
nums[i]
Why this works:
- With one element repeated
ntimes in a2narray, at least two occurrences must fall within any window of size4 - This guarantees an early detection without extra space
β±οΈ Complexity Analysis
-
Time Complexity:
O(n)β single traversal of the array -
Space Complexity:
O(1)β no additional data structures used
π§βπ» Code (JavaScript)
/**
* @param {number[]} nums
* @return {number}
*/
var repeatedNTimes = function(nums) {
for (let i = 0; i < nums.length - 1; i++) {
if (
nums[i] === nums[i + 1] ||
nums[i] === nums[i + 2] ||
nums[i] === nums[i + 3]
) {
return nums[i];
}
}
};
π― Reflection
This problem is a great reminder that:
-
Constraints often hide the real solution
-
Not every problem needs a hash map
-
Observation can reduce both time and space complexity
That wraps up Day 109 of my LeetCode challenge π₯
Onward to Day 110 β showing up daily beats solving everything at once π
Happy Coding π¨βπ»