Skip to content

Instantly share code, notes, and snippets.

@MorrisLaw
Last active January 22, 2022 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MorrisLaw/435d022cf1b5cc26050f6f4517565457 to your computer and use it in GitHub Desktop.
Save MorrisLaw/435d022cf1b5cc26050f6f4517565457 to your computer and use it in GitHub Desktop.
time complexity is O(n + m) where n is the first array and m is the second array. Space complexity is O(n) where n is the sizeof the map that gets created.
func intersect(nums1 []int, nums2 []int) []int {
count := make(map[int]int)
for _, n := range nums1 {
count[n]++
}
var result []int
for _, n := range nums2 {
if count[n] > 0 {
count[n]--
result = append(result, n)
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment