Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

961. N-Repeated Element in Size 2N Array - Leetcode Solution

Code Implementation

class Solution:
    def repeatedNTimes(self, nums):
        seen = set()
        for num in nums:
            if num in seen:
                return num
            seen.add(num)
class Solution {
public:
    int repeatedNTimes(vector<int>& nums) {
        unordered_set<int> seen;
        for (int num : nums) {
            if (seen.count(num)) return num;
            seen.insert(num);
        }
        return -1; // Should never reach here
    }
};
class Solution {
    public int repeatedNTimes(int[] nums) {
        HashSet<Integer> seen = new HashSet<>();
        for (int num : nums) {
            if (seen.contains(num)) return num;
            seen.add(num);
        }
        return -1; // Should not reach here
    }
}
var repeatedNTimes = function(nums) {
    const seen = new Set();
    for (const num of nums) {
        if (seen.has(num)) return num;
        seen.add(num);
    }
};

Problem Description

You are given an array nums of size 2N where there are exactly N+1 unique elements, and exactly one of these elements is repeated N times. All other elements appear only once. Your task is to find and return the element that is repeated N times.

  • There is exactly one valid solution for each input.
  • You must not reuse elements for counting; the repeated element must be detected by its frequency.
  • The input array can contain integers in any order.

For example, if nums = [1,2,3,3], then N = 2 and the answer is 3 because it appears twice.

Thought Process

To solve this problem, let's first understand what makes the repeated element special. The array has 2N elements, and among them, only one element appears N times, while the rest appear just once. This means the repeated element stands out by its frequency.

A brute-force approach would be to count the occurrences of every number in the array and return the one with count N. While this works, it's not the most efficient way, especially for larger arrays.

To optimize, we can leverage the guarantee that only one element is repeated, and it must appear at least twice. As we scan the array, the first element we see twice is guaranteed to be the answer. This is much faster because we can stop as soon as we find the repeated element, rather than counting all elements.

Solution Approach

  • Step 1: Create an empty set (or hash set) to keep track of elements we've seen so far.
  • Step 2: Iterate through each element in the nums array.
  • Step 3: For each element, check if it is already in the set:
    • If it is, we've found our repeated element and can return it immediately.
    • If not, add it to the set and continue.
  • Why use a set? Sets (or hash sets) allow for O(1) lookup and insertion time, making the process efficient.
  • Early exit: Since there's exactly one repeated element, we can stop as soon as we find it, which is faster than counting all elements.

This approach is both time and space efficient, and it directly leverages the problem's constraints.

Example Walkthrough

Let's walk through the solution with the input nums = [5,1,5,2,5,3,5,4].

  1. Initialize an empty set: seen = {}
  2. Read first number: 5. Not in seen. Add it: seen = {5}
  3. Read next: 1. Not in seen. Add it: seen = {1,5}
  4. Read next: 5. Already in seen! This is the repeated element. Return 5.

As you can see, the solution finds the answer quickly, without needing to check all elements or count frequencies for every number.

Time and Space Complexity

  • Brute-force approach: Count occurrences for each element (using nested loops or a hash map).
    • Time complexity: O(N) if using a hash map, O(N^2) if using nested loops.
    • Space complexity: O(N) for the hash map.
  • Optimized set-based approach (our solution):
    • Time complexity: O(N), because we scan the array once and set operations are O(1).
    • Space complexity: O(N), since in the worst case, we might store up to N+1 unique elements in the set.

The optimized approach is as efficient as possible for this problem, both in time and space.

Summary

In summary, we solved the N-Repeated Element in Size 2N Array problem by using a set to track seen elements as we scan the array. As soon as we encounter an element we've seen before, we return it as the answer. This leverages the problem's guarantee of exactly one repeated element, making the solution both simple and efficient. The key insight is to stop early and use fast set lookups, rather than counting every element's frequency.