I use the following code

```
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if (nums.empty()) return false;
if (k == 0) return false;
if (nums.size() - 1 <= k){
unordered_set<int> storage;
for (int i = 0; i <= k; i++){
if (!storage.insert(nums[i]).second) return true;
}
}
if (nums.size() - 1 > k){
unordered_set<int> storage;
for (int i = 0; i <= k; i++){
if (!storage.insert(nums[i]).second) return true;
}
unordered_set<int>::iterator it;
for (int i = k + 1; i<nums.size(); i++){
it = storage.begin();
storage.erase(it);
if (!storage.insert(nums[i]).second) return true;
}
}
return false;
}};
```

Then after submitting my answer it showed wrong answer for the test [1,2,1], k=1. However, when I use the same code and same inputs on my computer, I got the right answer. Any reason for this situation?