N-Repeated Element in Size 2N Array - Problem
Find the N-Repeated Element in Size 2N Array

You're given an integer array nums with some special properties that make this problem quite interesting:

• The array has exactly 2 * n elements
• There are n + 1 unique values in total
n values appear exactly once
One special element appears exactly n times

Your mission is to find that special element that appears n times. Since it appears half the time in the array, it's like finding the "dominant" element!

Example: In array [1,2,3,3] with length 4 (so n=2), the element 3 appears 2 times while others appear once.

Input & Output

example_1.py — Basic Case
$ Input: [1,2,3,3]
Output: 3
💡 Note: Array has length 4 (so n=2). Element 3 appears exactly 2 times while 1 and 2 appear once each.
example_2.py — Larger Array
$ Input: [2,1,2,5,3,2]
Output: 2
💡 Note: Array has length 6 (so n=3). Element 2 appears exactly 3 times while other elements appear once each.
example_3.py — Minimum Case
$ Input: [5,1,5,2,5,3,5,4]
Output: 5
💡 Note: Array has length 8 (so n=4). Element 5 appears exactly 4 times while elements 1,2,3,4 appear once each.

Constraints

  • 2 <= nums.length <= 104
  • 0 <= nums[i] <= 104
  • nums.length is even
  • nums contains n + 1 unique values
  • Exactly one element repeats n times

Visualization

Tap to expand
Finding the N-Repeated ElementArray: [1, 2, 3, 3]1233Duplicate!Detection Process:Step 1: See '1' → Add to set {1}Step 2: See '2' → Add to set {1,2}Step 3: See '3' → Add to set {1,2,3}Step 4: See '3' → Already in set!→ Return 3 immediately✓ Found in O(n) time✓ Early termination✓ Optimal solution💡 Key InsightSince exactly one element repeats n times, the first duplicatewe encounter must be our answer!
Understanding the Visualization
1
Start Scanning
Begin checking each element in the array
2
Track Seen Elements
Use hash set to remember which elements we've encountered
3
Detect Duplicate
First element we see twice is our repeated element
4
Early Return
Return immediately - no need to check remaining elements
Key Takeaway
🎯 Key Insight: With only one element repeating exactly n times, any duplicate detection immediately gives us the answer!
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
42.0K Views
Medium Frequency
~8 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen