-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2956-FindCommonElementsBetweenTwoArrays.go
More file actions
119 lines (106 loc) · 3.84 KB
/
2956-FindCommonElementsBetweenTwoArrays.go
File metadata and controls
119 lines (106 loc) · 3.84 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
// 2956. Find Common Elements Between Two Arrays
// You are given two integer arrays nums1 and nums2 of sizes n and m, respectively.
// Calculate the following values:
// answer1 : the number of indices i such that nums1[i] exists in nums2.
// answer2 : the number of indices i such that nums2[i] exists in nums1.
// Return [answer1,answer2].
// Example 1:
// Input: nums1 = [2,3,2], nums2 = [1,2]
// Output: [2,1]
// Explanation:
// <img scr="https://assets.leetcode.com/uploads/2024/05/26/3488_find_common_elements_between_two_arrays-t1.gif" />
// Example 2:
// Input: nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
// Output: [3,4]
// Explanation:
// The elements at indices 1, 2, and 3 in nums1 exist in nums2 as well. So answer1 is 3.
// The elements at indices 0, 1, 3, and 4 in nums2 exist in nums1. So answer2 is 4.
// Example 3:
// Input: nums1 = [3,4,2,3], nums2 = [1,5]
// Output: [0,0]
// Explanation:
// No numbers are common between nums1 and nums2, so answer is [0,0].
// Constraints:
// n == nums1.length
// m == nums2.length
// 1 <= n, m <= 100
// 1 <= nums1[i], nums2[i] <= 100
import "fmt"
func findIntersectionValues(nums1 []int, nums2 []int) []int {
mp1, mp2 := make(map[int]bool), make(map[int]bool)
res1, res2 := 0, 0
for _, v := range nums1 { mp1[v] = true }
for _, v := range nums2 { mp2[v] = true }
for _, v := range nums1 {
if _, ok := mp2[v]; ok {
res1++
}
}
for _, v := range nums2 {
if _, ok := mp1[v]; ok {
res2++
}
}
return []int{ res1, res2 }
}
func findIntersectionValues1(nums1 []int, nums2 []int) []int {
n, m := len(nums1), len(nums2)
res := make([]int, 2)
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
if nums1[i] == nums2[j] {
res[0]++
break
}
}
}
for j := 0; j < m; j++ {
for i := 0; i < n; i++ {
if nums2[j] == nums1[i] {
res[1]++
break
}
}
}
return res
}
func findIntersectionValues2(nums1 []int, nums2 []int) []int {
res, arr1, arr2 := make([]int,2), make([]int, 101), make([]int, 101)
for _, v := range nums1 { arr1[v]++ }
for _, v := range nums2 { arr2[v]++ }
for i := 1; i < 101; i++ {
if arr1[i] != 0 && arr2[i] != 0 {
res[0] += arr1[i]
res[1] += arr2[i]
}
}
return res
}
func main() {
// Example 1:
// Input: nums1 = [2,3,2], nums2 = [1,2]
// Output: [2,1]
// Explanation:
// <img scr="https://assets.leetcode.com/uploads/2024/05/26/3488_find_common_elements_between_two_arrays-t1.gif" />
fmt.Println(findIntersectionValues([]int{2,3,2}, []int{1,2})) // [2,1]
// Example 2:
// Input: nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
// Output: [3,4]
// Explanation:
// The elements at indices 1, 2, and 3 in nums1 exist in nums2 as well. So answer1 is 3.
// The elements at indices 0, 1, 3, and 4 in nums2 exist in nums1. So answer2 is 4.
fmt.Println(findIntersectionValues([]int{4,3,2,3,1}, []int{2,2,5,2,3,6})) // [3,4]
// Example 3:
// Input: nums1 = [3,4,2,3], nums2 = [1,5]
// Output: [0,0]
// Explanation:
// No numbers are common between nums1 and nums2, so answer is [0,0].
fmt.Println(findIntersectionValues([]int{3,4,2,3}, []int{1,5})) // [0,0]
fmt.Println(findIntersectionValues1([]int{2,3,2}, []int{1,2})) // [2,1]
fmt.Println(findIntersectionValues1([]int{4,3,2,3,1}, []int{2,2,5,2,3,6})) // [3,4]
fmt.Println(findIntersectionValues1([]int{3,4,2,3}, []int{1,5})) // [0,0]
fmt.Println(findIntersectionValues2([]int{2,3,2}, []int{1,2})) // [2,1]
fmt.Println(findIntersectionValues2([]int{4,3,2,3,1}, []int{2,2,5,2,3,6})) // [3,4]
fmt.Println(findIntersectionValues2([]int{3,4,2,3}, []int{1,5})) // [0,0]
}