N-Repeated Element in Size 2N Array Problem & Solution
You are given an integer array nums
with the following properties:
nums.length == 2 * n
.nums
containsn + 1
unique elements.- Exactly one element of
nums
is repeatedn
times.
Return the element that is repeated n
times.
See the n-repeated element in size 2n array problem on LeetCode.
C++ Solution
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("avx,avx2,fma")
static const int _=[](){std::ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();
class Solution {
public:
int repeatedNTimes(vector<int>& nums) {
for (int i = 2; i < nums.size(); ++i) {
if (nums[i - 2] == nums[i] || nums[i - 1] == nums[i]) {
return nums[i];
}
}
return nums[0];
}
};