-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution1207.go
More file actions
47 lines (41 loc) · 957 Bytes
/
Copy pathsolution1207.go
File metadata and controls
47 lines (41 loc) · 957 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package solution1207
import (
"math"
)
// ============================================================================
// 1207. Unique Number of Occurrences
// URL: https://leetcode.com/problems/unique-number-of-occurrences/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/1207---Unique-Number-of-Occurrences
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_unique-24 4219881 246.4 ns/op 48 B/op 2 allocs/op
PASS
*/
func uniqueOccurrences(arr []int) bool {
freq := make(map[int]int, len(arr))
for _, val := range arr {
_, ok := freq[val]
if !ok {
freq[val] = 1
} else {
freq[val]++
}
}
sl := make([]int, 2002)
for i, v := range freq {
if v < 0 {
v = int(math.Abs(float64(v))) + 1
} else {
v += 1001
}
if sl[v] != 0 {
return false
}
sl[v] = i
}
return true
}