LeetCode Solutions
961. N-Repeated Element in Size 2N Array
Time: $O(n)$ Space: $O(1)$
class Solution {
public:
int repeatedNTimes(vector<int>& A) {
for (int i = 0; i + 2 < A.size(); ++i)
if (A[i] == A[i + 1] || A[i] == A[i + 2])
return A[i];
return A.back();
}
};
class Solution {
public int repeatedNTimes(int[] A) {
for (int i = 0; i + 2 < A.length; ++i)
if (A[i] == A[i + 1] || A[i] == A[i + 2])
return A[i];
return A[A.length - 1];
}
}
class Solution:
def repeatedNTimes(self, A: List[int]) -> int:
for i in range(len(A) - 2):
if A[i] == A[i + 1] or A[i] == A[i + 2]:
return A[i]
return A[-1]