Skip to content

Commit fbf44c3

Browse files
Add comment to containsNearbyDuplicateHashSet method
Added a comment to clarify the method's purpose.
1 parent 9283b78 commit fbf44c3

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class containsDuplicateHashSet {
2+
public boolean containsNearbyDuplicateHashSet(int[] nums, int k) { // Optimal Solution using HashSet
3+
4+
HashSet<Integer> set = new HashSet<>();
5+
6+
for(int i = 0; i<nums.length; i++){
7+
if(i >= k+1){
8+
set.remove(nums[i-k-1]);
9+
if(!set.contains(nums[i])){
10+
set.add(nums[i]);
11+
}
12+
else{
13+
return true;
14+
}
15+
}
16+
else if(i<k+1){
17+
if(!set.contains(nums[i])){
18+
set.add(nums[i]);
19+
}
20+
else{
21+
return true;
22+
}
23+
}
24+
}
25+
26+
return false;
27+
}
28+
}

0 commit comments

Comments
 (0)