MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / containsNearbyDuplicate

Method containsNearbyDuplicate

java/0219-contains-duplicate-ii.java:2–21  ·  view source on GitHub ↗
(int[] nums, int k)

Source from the content-addressed store, hash-verified

1class Solution {
2 public boolean containsNearbyDuplicate(int[] nums, int k) {
3
4 Set<Integer> window = new HashSet<>();
5 int left = 0;
6 for (int right = 0; right < nums.length; right++) {
7
8 if (window.size() > k) {
9 window.remove(nums[left]);
10 left++;
11 }
12
13 if (window.contains(nums[right])) {
14 return true;
15 }
16
17 window.add(nums[right]);
18 }
19
20 return false;
21 }
22}

Callers

nothing calls this directly

Calls 4

sizeMethod · 0.45
removeMethod · 0.45
containsMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected