Intersection of Two Arrays II

DSA Solving and practicing

Posted by Hitesh Kumar on June 23, 2022
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

func intersect(nums1 []int, nums2 []int) []int {
    intersection1 := []int{}
	mapforone := make(map[int]int)

	for i := 0; i < len(nums1); i++ {
		mapforone[nums1[i]]++
	}

	for i := 0; i < len(nums2); i++ {
		if mapforone[nums2[i]] > 0 {
			intersection1 = append(intersection1, nums2[i])
			mapforone[nums2[i]]--
		}
	}
    
    return intersection1
    
}


Screenshot-2022-06-24-at-22-31-56-Intersection-of-Two-Arrays-II-Leet-Code.png