Skip to content

Commit 6d65a91

Browse files
Implement Contains Duplicate - II
1 parent 8e76bd1 commit 6d65a91

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class ContainsDuplicateII {
2+
public boolean containsNearbyDuplicate(int[] nums, int k) {
3+
4+
Map<Integer, Integer> map = new TreeMap<>();
5+
6+
for(int i = 0; i<nums.length; i++){
7+
if(!map.containsKey(nums[i])){
8+
map.put(nums[i], i);
9+
}
10+
else if(map.containsKey(nums[i]) && Math.abs(map.get(nums[i]) - i) <= k){
11+
return true;
12+
}
13+
else{
14+
map.put(nums[i], i);
15+
}
16+
}
17+
18+
return false;
19+
}
20+
}

0 commit comments

Comments
 (0)