Skip to content

Commit 62ad6fd

Browse files
committed
solved: 219. Contains Duplicate II
Signed-off-by: rajput-hemant <[email protected]>
1 parent b71cae1 commit 62ad6fd

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
func containsNearbyDuplicate(nums []int, k int) bool {
4+
if len(nums) <= 1 {
5+
return false
6+
}
7+
8+
set := make(map[int]int)
9+
10+
// iterate over the array
11+
for i, v := range nums {
12+
// check if the value is in the set
13+
if _, ok := set[v]; ok {
14+
// if the value is in the set,
15+
// check if the difference between the current index and the index of the value in the set
16+
// is less than or equal to k
17+
if i-set[v] <= k {
18+
return true
19+
}
20+
}
21+
// add the value to the set
22+
set[v] = i
23+
}
24+
25+
return false
26+
}

0 commit comments

Comments
 (0)