-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution2215.go
More file actions
58 lines (48 loc) · 1.2 KB
/
Copy pathsolution2215.go
File metadata and controls
58 lines (48 loc) · 1.2 KB
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
package solution2215
import (
"slices"
)
// ============================================================================
// 2215. Find the Difference of Two Arrays
// URL: https://leetcode.com/problems/find-the-difference-of-two-arrays/
// ============================================================================
/*
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/2215---Find-the-Difference-of-Two-Arrays
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_findDifference
Benchmark_findDifference-24 6763864 177.0 ns/op 96 B/op 3 allocs/op
PASS
*/
func findDifference(nums1 []int, nums2 []int) [][]int {
freq1 := make(map[int]int, len(nums1))
for _, num := range nums1 {
freq1[num]++
}
freq2 := make(map[int]int, len(nums2))
for _, num := range nums2 {
freq2[num]++
}
dist1 := make([]int, 0, len(nums1))
dist2 := make([]int, 0, len(nums2))
for _, num := range nums1 {
if slices.Contains(dist1, num) {
continue
}
_, ok := freq2[num]
if !ok {
dist1 = append(dist1, num)
}
}
for _, num := range nums2 {
if slices.Contains(dist2, num) {
continue
}
_, ok := freq1[num]
if !ok {
dist2 = append(dist2, num)
}
}
return [][]int{dist1, dist2}
}