Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created August 28, 2017 13:43
Show Gist options
  • Save luojiyin1987/5b27d7a0ca05af869079f53034a6a9ce to your computer and use it in GitHub Desktop.
Save luojiyin1987/5b27d7a0ca05af869079f53034a6a9ce to your computer and use it in GitHub Desktop.
Intersection of Two Arrays II
func intersect(nums1 []int, nums2 []int) []int {
temp1 := make(map[int]int)
temp2 :=make(map[int]int)
result := []int{}
for _, v := range nums1 {
temp1[v]++
}
for _, v := range nums2 {
temp2[v]++
}
for k, value := range temp1{
_, ok2 := temp2[k]
if ok2 {
min := 0
if value < temp2[k] {
min = value
}else {
min = temp2[k]
}
for i :=0 ; i < min ; i++ {
result = append(result, k)
}
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment