C++ 代码
class Solution {
public:
int duplicateInArray(vector<int>& nums) {
int n = nums.size();
for (auto v : nums)
if (v < 0 || v > n - 1)
return -1;
unordered_set<int> S;
for (auto v : nums)
if (S.count(v))
return v;
else
S.insert(v);
return -1;
}
};