Skip to content

Commit a93023a

Browse files
authored
Merge pull request SjxSubham#156 from VN1702/main
219. Containing Nearby Duplicate Problem
2 parents 512aa15 + dc57ec4 commit a93023a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

219.NearbyDuplicate.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <vector>
2+
#include <unordered_map>
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
bool containsNearbyDuplicate(vector<int>& nums, int k) {
8+
int n=nums.size();
9+
unordered_map<int,int> indexmap;
10+
11+
for(int i=0;i<n;i++){
12+
int cur_num=nums[i];
13+
if(indexmap.count(cur_num)){
14+
int prev_index=indexmap[cur_num];
15+
if(i-prev_index<=k){
16+
return true;
17+
}
18+
}
19+
indexmap[cur_num]=i;
20+
}
21+
22+
return false;
23+
}
24+
};

0 commit comments

Comments
 (0)