Back to posts

LeetCode Challenge Day 109 β€” 961. N-Repeated Element in Size 2N Array

Nitin Ahirwal / January 2, 2026

LeetCode ChallengeDay 109ArraysObservationJavaScriptEasy

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 nums of length 2n
  • Exactly n + 1 unique elements
  • One element is repeated exactly n times

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

  1. Traverse the array from left to right
  2. For each index i, compare nums[i] with:
    • nums[i + 1]
    • nums[i + 2]
    • nums[i + 3]
  3. If any match is found, return nums[i]

Why this works:

  • With one element repeated n times in a 2n array, at least two occurrences must fall within any window of size 4
  • 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 πŸ‘¨β€πŸ’»