Last active
January 22, 2022 13:08
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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