← Home

961. N-Repeated Element in Size 2N Array

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
961

This is one of those problems where the clean idea matters more than the amount of code. For 961. N-Repeated Element in Size 2N Array, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are repeatedNTimes.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • A set is doing the membership or uniqueness work, which keeps the main loop readable.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01/**
02In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times.
03
04Return the element repeated N times.
05
06 
07
08Example 1:
09
10Input: [1,2,3,3]
11Output: 3
12Example 2:
13
14Input: [2,1,2,5,3,2]
15Output: 2
16Example 3:
17
18Input: [5,1,5,2,5,3,5,4]
19Output: 5
20 
21
22Note:
23
244 <= A.length <= 10000
250 <= A[i] < 10000
26A.length is even
27**/
28
29//Runtime: 44 ms, faster than 50.05% of C++ online submissions for N-Repeated Element in Size 2N Array.
30class Solution {
31public:
32    int repeatedNTimes(vector<int>& A) {
33        set<int> set;
34        for(int i = 0; i<A.size(); i++){
35            if(set.find(A[i])==set.end()){
36                set.insert(A[i]);
37            }else{
38                return A[i];
39            }
40        }
41    }
42};
43
44/**
45Approach 1: Count
46Intuition and Algorithm
47
48Let's count the number of elements. We can use a HashMap or an array - here, we use a HashMap.
49
50After, the element with a count larger than 1 must be the answer.
51
52//Java
53class Solution {
54    public int repeatedNTimes(int[] A) {
55        Map<Integer, Integer> count = new HashMap();
56        for (int x: A) {
57            count.put(x, count.getOrDefault(x, 0) + 1);
58        }
59
60        for (int k: count.keySet())
61            if (count.get(k) > 1)
62                return k;
63
64        throw null;
65    }
66}
67
68Complexity Analysis
69
70Time Complexity: O(N), where N is the length of A.
71
72Space Complexity: O(N). 
73**/
74
75/**
76Approach 2: Compare
77Intuition and Algorithm
78
79If we ever find a repeated element, it must be the answer. Let's call this answer the major element.
80
81Consider all subarrays of length 4. There must be a major element in at least one such subarray.
82
83This is because either:
84
85There is a major element in a length 2 subarray, or;
86Every length 2 subarray has exactly 1 major element, which means that a length 4 subarray that begins at a major element will have 2 major elements.
87Thus, we only have to compare elements with their neighbors that are distance 1, 2, or 3 away.
88
89//Runtime: 48 ms, faster than 35.71% of C++ online submissions for N-Repeated Element in Size 2N Array.
90class Solution {
91public:
92    int repeatedNTimes(vector<int>& A) {
93        for(int k = 1; k <= 3; k++){
94            for(int i = 0; i < A.size() - k; i++){
95                if(A[i]==A[i+k]){
96                    return A[i];
97                }
98            }
99        }
100    }
101};
102
103Complexity Analysis
104
105Time Complexity: O(N), where N is the length of A.
106
107Space Complexity: O(1). 
108**/

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.