Skip to content

Instantly share code, notes, and snippets.

@spcbfr
Created March 1, 2024 05:54
Show Gist options
  • Save spcbfr/b57ac41ed9d454ad467607c3144a63e6 to your computer and use it in GitHub Desktop.
Save spcbfr/b57ac41ed9d454ad467607c3144a63e6 to your computer and use it in GitHub Desktop.
Two Sum solved in golang with hashmaps
func twoSum(nums []int, target int) []int {
m := make(map[int]int)
for i := 0; i < len(nums); i++ {
curr := nums[i]
diff := target - curr
if _, ok := m[diff]; ok {
return []int{m[diff], i}
}
m[curr] = i
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment