Problem List

Two Sum

September 26, 2024Go array, hashmap

This is one of the most common intro questions to test problem solving and hashmap use.

Go Solution
func twoSum(nums []int, target int) []int {
	hashMap := make(map[int]int)

	for i, num := range nums {
		difference := target - num

		if targetIdx, found := hashMap[difference]; found {
			return []int{targetIdx, i}
		} else {
            hashMap[num] = i
        }
	}

    return []int{-1,-1}
}
LeetCode Problem Link