What if you could find matching pairs instantly without checking every possibility?
Why Two Sum Problem Classic Hash Solution in DSA Go?
Imagine you have a list of prices for items in a store, and you want to find two items that together cost exactly your gift card amount. Doing this by checking every pair one by one is like trying to find a friend in a crowd by asking each person individually--it takes a lot of time and effort.
Checking every possible pair manually means you spend a lot of time, especially if the list is long. It's easy to make mistakes or miss pairs because you have to remember which items you already checked. This slow and error-prone way makes you frustrated and wastes your time.
The classic hash solution uses a simple map to remember what numbers you've seen. Instead of checking all pairs, you look up if the number needed to reach your target is already in the map. This way, you find the answer quickly and without confusion, like having a friend who instantly tells you if your match is nearby.
for i := 0; i < len(nums); i++ { for j := i + 1; j < len(nums); j++ { if nums[i]+nums[j] == target { return []int{i, j} } } }
seen := make(map[int]int) for i, num := range nums { complement := target - num if j, found := seen[complement]; found { return []int{j, i} } seen[num] = i }
This method lets you find pairs that add up to a target instantly, even in huge lists, saving time and effort.
When shopping online, you want to quickly find two products that fit your budget exactly without trying every combination manually.
Manual pair checking is slow and error-prone.
Using a hash map remembers seen numbers for quick lookup.
This approach finds pairs in one pass, making it fast and simple.
Practice
Two Sum Problem Classic Hash Solution?Solution
Step 1: Understand the problem goal
The goal is to find two numbers in the array that add up to a target value.Step 2: Use a hash map for efficient lookup
By storing each number and its index in a hash map, we can check if the complement (target - current number) exists quickly.Final Answer:
Use a hash map to store numbers and their indices for quick lookup -> Option AQuick Check:
Hash map lookup = fast pair finding [OK]
- Trying to use nested loops which is slower
- Sorting the array which is unnecessary here
- Not storing indices with numbers
Solution
Step 1: Recall Go map initialization syntax
Maps in Go are initialized using the built-in make function like make(map[keyType]valueType).Step 2: Identify correct syntax
m := make(map[int]int) uses make(map[int]int) which is the correct way to create an empty map with int keys and int values.Final Answer:
m := make(map[int]int) -> Option CQuick Check:
make(map[int]int) = correct map init [OK]
- Using var without make causes nil map
- Incorrect syntax like map[int]int(make())
- Using empty braces without make in some contexts
func twoSum(nums []int, target int) []int {
m := make(map[int]int)
for i, num := range nums {
if j, ok := m[target-num]; ok {
return []int{j, i}
}
m[num] = i
}
return nil
}
fmt.Println(twoSum([]int{2, 7, 11, 15}, 9))Solution
Step 1: Trace the loop and map updates
At i=0, num=2, map is empty, so store m[2]=0.
At i=1, num=7, check if m[9-7=2] exists, yes at index 0.
Return [0,1].Step 2: Confirm returned indices
The function returns indices of numbers 2 and 7 which sum to 9, so output is [0 1].Final Answer:
[0 1] -> Option BQuick Check:
Indices of 2 and 7 = [0 1] [OK]
- Returning indices in wrong order
- Not checking map before insertion
- Returning nil when pair exists
func twoSum(nums []int, target int) []int {
m := make(map[int]int)
for i, num := range nums {
if j, ok := m[num-target]; ok {
return []int{j, i}
}
m[num] = i
}
return nil
}Solution
Step 1: Analyze complement calculation
The complement to find is target - current number, not num - target, because we want a number that adds with num to target.Step 2: Identify the fix
Change m[num-target] to m[target-num] to correctly check for the complement in the map.Final Answer:
The complement calculation should be target - num, not num - target -> Option AQuick Check:
Complement = target - num [OK]
- Swapping target and num in complement
- Incorrect map initialization
- Returning nil too early
nums = [3, 3, 4, 2] and target 6, what will the Two Sum Classic Hash Solution return?Consider the code below:
func twoSum(nums []int, target int) []int {
m := make(map[int]int)
for i, num := range nums {
if j, ok := m[target-num]; ok {
return []int{j, i}
}
m[num] = i
}
return nil
}Solution
Step 1: Trace the loop with given input
i=0, num=3, map empty, store m[3]=0.
i=1, num=3, check m[6-3=3], found at index 0, return [0,1].Step 2: Confirm returned indices
Indices 0 and 1 correspond to values 3 and 3 which sum to 6, so output is [0 1].Final Answer:
[0 1] -> Option DQuick Check:
3 + 3 = 6 at indices [0 1] [OK]
- Assuming different pairs without checking map
- Returning nil when pair exists
- Confusing indices order
Ready to prove it?
Pass the test and this pattern moves unknown
