-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution219.go
More file actions
29 lines (25 loc) · 777 Bytes
/
solution219.go
File metadata and controls
29 lines (25 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package solution219
// ============================================================================
// 219. Contains Duplicate II
// URL: https://leetcode.com/problems/contains-duplicate-ii/
// ============================================================================
/*
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/219---Contains-Duplicate-II
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_containsNearbyDuplicate
Benchmark_containsNearbyDuplicate-24 31926517 31.42 ns/op 0 B/op 0 allocs/op
PASS
*/
func containsNearbyDuplicate(nums []int, k int) bool {
track := make(map[int]int, len(nums))
for i, v := range nums {
index, ok := track[v]
if ok && (i-index) <= k {
return true
}
track[v] = i
}
return false
}