Skip to content

Instantly share code, notes, and snippets.

@MorrisLaw
Last active January 20, 2022 22:19
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/24dc545dc52bec4583dc2b0f07a5a8f7 to your computer and use it in GitHub Desktop.
Save MorrisLaw/24dc545dc52bec4583dc2b0f07a5a8f7 to your computer and use it in GitHub Desktop.
time complexity is O(n) where n is the max number of elements we look at to check and store in the set and O(n) space complexity because the set is proportionate to the number of elements in the array we're looking at, all of them in the worse case.
func containsDuplicate(nums []int) bool {
if nums == nil {
return false
}
set := make(map[int]bool)
for _, i := range nums {
if set[i] {
return true
}
set[i] = true
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment