-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution350.go
71 lines (66 loc) · 1.31 KB
/
solution350.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package solution350
// ============================================================================
// 350. Intersection of Two Arrays II
// URL: https://leetcode.com/problems/intersection-of-two-arrays-ii/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_intersection-24 7460503 171.3 ns/op 72 B/op 4 allocs/op
PASS
*/
func intersection(nums1 []int, nums2 []int) []int {
ans := []int{}
m1 := make(map[int]int, len(nums1))
m2 := make(map[int]int, len(nums2))
for i := 0; i < len(nums1); i++ {
val := nums1[i]
_, ok := m1[val]
if !ok {
m1[val] = 1
} else {
m1[val]++
}
}
for i := 0; i < len(nums2); i++ {
val := nums2[i]
_, ok := m2[val]
if !ok {
m2[val] = 1
} else {
m2[val]++
}
}
if len(m1) < len(m2) {
for k, v1 := range m1 {
v2, ok := m2[k]
minval := v2
if !ok {
continue
}
if v1 < minval {
minval = v1
}
for i := 0; i < minval; i++ {
ans = append(ans, k)
}
}
} else {
for k, v2 := range m2 {
v1, ok := m1[k]
minval := v1
if !ok {
continue
}
if v2 < minval {
minval = v2
}
for i := 0; i < minval; i++ {
ans = append(ans, k)
}
}
}
return ans
}