We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b71cae1 commit 62ad6fdCopy full SHA for 62ad6fd
src/0201-0300/219 - Contains Duplicate II/contains_duplicate_II.go
@@ -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
26
+}
0 commit comments