-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathintersection.go
More file actions
44 lines (37 loc) · 822 Bytes
/
Copy pathintersection.go
File metadata and controls
44 lines (37 loc) · 822 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
/* https://leetcode.com/problems/intersection-of-two-arrays/#/description
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
Note:
Each element in the result must be unique.
The result can be in any order.
*/
package lht
func intersection(nums1 []int, nums2 []int) []int {
m, n := len(nums1), len(nums2)
if m == 0 || n == 0 {
return []int{}
}
if m > n {
m, n = n, len(nums1)
temp := nums1
nums1, nums2 = nums2, temp
}
maps, r := make(map[int]int, m), make([]int, m)
for i := range nums1 {
if _, ok := maps[nums1[i]]; !ok {
maps[nums1[i]] = 1
}
}
c := 0
for i := range nums2 {
if v, ok := maps[nums2[i]]; ok {
maps[nums2[i]] = v + 1
if v+1 == 2 {
r[c] = nums2[i]
c++
}
}
}
return r[:c]
}