8000 feat: solved 350. Intersection of Two Arrays II Β· realtemirov/leetcode@944ce71 Β· GitHub
[go: up one dir, main page]

Skip to content

Commit 944ce71

Browse files
committed
feat: solved 350. Intersection of Two Arrays II
1 parent 6992d1c commit 944ce71

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 350. Intersection of Two Arrays II
2+
3+
🟩 Easy
4+
5+
## Solution
6+
7+
My Solution
8+
9+
```go
10+
func intersect(nums1 []int, nums2 []int) []int {
11+
var m map[int]int
12+
if len(nums1) < len(nums2) {
8000 13+
m = make(map[int]int, len(nums1))
14+
for _, num := range nums1 {
15+
m[num]++
16+
}
17+
} else {
18+
m = make(map[int]int, len(nums2))
19+
for _, num := range nums2 {
20+
m[num]++
21+
}
22+
}
23+
24+
if len(nums1) < len(nums2) {
25+
nums1 = nums2
26+
}
27+
28+
nums := make([]int, 0, len(m))
29+
for _, num := range nums1 {
30+
if m[num] != 0 {
31+
nums = append(nums, num)
32+
m[num]--
33+
}
34+
}
35+
return nums
36+
}
37+
```
38+
39+
![result](350.png)
40+
41+
Leetcode: [link](https://leetcode.com/problems/intersection-of-two-arrays-ii/)

β€ŽProblems/0350/350.png

30.4 KB
Loading

β€ŽProblems/0350/solution.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package problem0350
2+
3+
func Intersect(nums1 []int, nums2 []int) []int {
4+
var m map[int]int
5+
if len(nums1) < len(nums2) {
6+
m = make(map[int]int, len(nums1))
7+
for _, num := range nums1 {
8+
m[num]++
9+
}
10+
} else {
11+
m = make(map[int]int, len(nums2))
12+
for _, num := range nums2 {
13+
m[num]++
14+
}
15+
}
16+
17+
if len(nums1) < len(nums2) {
18+
nums1 = nums2
19+
}
20+
21+
nums := make([]int, 0, len(m))
22+
for _, num := range nums1 {
23+
if m[num] != 0 {
24+
nums = append(nums, num)
25+
m[num]--
26+
}
27+
}
28+
return nums
29+
}

β€ŽProblems/0350/solution_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package problem0350_test
2+
3+
import (
4+
"testing"
5+
6+
problem0350 "github.com/realtemirov/leetcode/Problems/0350"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestSolution(t *testing.T) {
11+
testCases := []struct {
12+
name string
13+
cases [][]int
14+
expected []int
15+
}{
16+
{
17+
name: "Test 1",
18+
cases: [][]int{{1, 2, 2, 1}, {2, 2}},
19+
expected: []int{2, 2},
20+
},
21+
{
22+
name: "Test 2",
23+
cases: [][]int{{4, 9, 5}, {9, 4, 9, 8, 4}},
24+
expected: []int{9, 4},
25+
},
26+
}
27+
28+
for _, tc := range testCases {
29+
t.Run(tc.name, func(t *testing.T) {
30+
result := problem0350.Intersect(tc.cases[0], tc.cases[1])
31+
require.Equal(t, tc.expected, result, "expected: %v, result: %v", tc.expected, result)
32+
})
33+
}
34+
}

β€ŽProblems/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* 🟩 Easy - [242. Valid Anagram](<./0242/242. Valid Anagram.md>)
4646
* 🟩 Easy - [268. Missing Number](<./0268/268. Missing Number.md>)
4747
* 🟩 Easy - [283. Move Zeroes](<./0283/283. Move Zeroes.md>)
48+
* 🟩 Easy - [350. Intersection of Two Arrays II](<./0350/350. Intersection of Two Arrays II.md>)
4849
* 🟩 Easy - [383. Ransom Note](<./0383/383. Ransom Note.md>)
4950
* 🟩 Easy - [389. Find the Difference](<./0389/389. Find the Difference.md>)
5051
* 🟩 Easy - [412. Fizz Buzz](<./0412/412. Fizz Buzz.md>)

β€ŽSUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
* [242. Valid Anagram](<./Problems/0242/242. Valid Anagram.md>)
101101
* [268. Missing Number](<./Problems/0268/268. Missing Number.md>)
102102
* [283. Move Zeroes](<./Problems/0283/283. Move Zeroes.md>)
103+
* [350. Intersection of Two Arrays II](<./Problems/0350/350. Intersection of Two Arrays II.md>)
103104
* [383. Ransom Note](<./Problems/0383/383. Ransom Note.md>)
104105
* [389. Find the Difference](<./Problems/0389/389. Find the Difference.md>)
105106
* [412. Fizz Buzz](<./Problems/0412/412. Fizz Buzz.md>)

0 commit comments

Comments
Β (0)
0