Skip to content

Commit dfad7d1

Browse files
committed
completed 2248 - intersecton of multiple arrays in go
1 parent 2dc1749 commit dfad7d1

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 2248. Intersecton of Multiple Arrays
2+
3+
Given a 2D integer array nums where nums[i] is a non-empty array of distinct positive integers, return the list of integers that are present in each array of nums sorted in ascending order.
4+
5+
6+
Example 1:
7+
8+
Input: nums = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]]
9+
Output: [3,4]
10+
Explanation:
11+
The only integers present in each of nums[0] = [3,1,2,4,5], nums[1] = [1,2,3,4], and nums[2] = [3,4,5,6] are 3 and 4, so we return [3,4].
12+
13+
14+
15+
Example 2:
16+
17+
Input: nums = [[1,2,3],[4,5,6]]
18+
Output: []
19+
Explanation:
20+
There does not exist any integer present both in nums[0] and nums[1], so we return an empty list [].
21+
22+
23+
Constraints:
24+
25+
```text
26+
1 <= nums.length <= 1000
27+
1 <= sum(nums[i].length) <= 1000
28+
1 <= nums[i][j] <= 1000
29+
All the values of nums[i] are unique.
30+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module intersectionOfMultipleArrays
2+
3+
go 1.23.2
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package solution
2+
3+
import "slices"
4+
5+
func intersection(nums [][]int) []int {
6+
// create a map to count occurrences of each number
7+
counts := make(map[int]int)
8+
9+
// iterate through each array in nums
10+
for _, n := range nums {
11+
// increment the count for each number in the sub-slice
12+
for _, v := range n {
13+
counts[v]++
14+
}
15+
}
16+
17+
// store the number of input slices
18+
n := len(nums)
19+
20+
// slice to store the numbers that appear in all slices
21+
answer := []int{}
22+
23+
// iterate through the counts map
24+
for k, v := range counts {
25+
// if a number appears in all slices (count equals n)
26+
if v == n {
27+
// add it to the answer slice
28+
answer = append(answer, k)
29+
}
30+
}
31+
32+
// sort the answser slice in ascending order
33+
slices.Sort(answer)
34+
35+
// return the answer slice
36+
return answer
37+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package solution
2+
3+
import (
4+
"slices"
5+
"testing"
6+
)
7+
8+
func TestIntersection(t *testing.T) {
9+
testCases := []struct {
10+
name string
11+
nums [][]int
12+
want []int
13+
}{
14+
{
15+
name: "test case one",
16+
nums: [][]int{{3, 1, 2, 4, 5}, {1, 2, 3, 4}, {3, 4, 5, 6}},
17+
want: []int{3, 4},
18+
},
19+
{
20+
name: "test case two",
21+
nums: [][]int{{1, 2, 3}, {4, 5, 6}},
22+
want: []int{},
23+
},
24+
}
25+
26+
for _, tc := range testCases {
27+
t.Run(tc.name, func(t *testing.T) {
28+
got := intersection(tc.nums)
29+
30+
if !slices.Equal(got, tc.want) {
31+
t.Errorf("got %v, want %v", got, tc.want)
32+
}
33+
})
34+
}
35+
}

0 commit comments

Comments
 (0)