By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
You're sitting in an interview and the interviewer asks you to find two numbers in an array that add up to a target. Your first instinct is nested loops—O(n²). But there's a better way. Arrays and hash maps are the foundation for optimizing these problems from brute force to linear time.
Not all array operations are created equal. Index access is O(1)—you jump directly to the element. Appending to the end is O(1) amortised. But inserting at the front or searching for a value requires scanning, which is O(n).
nums = [3, 1, 4, 1, 5]
nums[2] # O(1) — index access
nums.append(9) # O(1) amortised — add to end
nums.insert(0,7) # O(n) — shifts everything right
"4" in nums # O(n) — linear scan
nums.pop() # O(1) — remove from end
nums.pop(0) # O(n) — remove from frontThis matters because it shapes your solution strategy. If you need to check membership or remove from the front repeatedly, an array becomes expensive. That's when you reach for a different tool.
A hash map uses a hash function to convert keys into array indices. This gives you O(1) average-case insert, lookup, and delete. It's the secret weapon for converting O(n²) brute-force solutions into O(n).
The classic use case is counting frequencies. You iterate through a collection once, storing each element's count in the map. One pass, done.
# Counting frequencies — classic hash map pattern
def char_frequency(s):
freq = {}
for ch in s:
freq[ch] = freq.get(ch, 0) + 1
return freq
print(char_frequency("mississippi"))
# {"m":1, "i":4, "s":4, "p":2}Many array problems follow the same structure: for each element, have I seen its complement already? Store what you've seen in a hash map indexed by value, then check before processing each new element.
Two Sum is the canonical example. Instead of checking every pair (O(n²)), you iterate once. For each number, you check if its complement exists in the map. If yes, return. If no, store the number and move on.
# Two Sum — O(n) using complement pattern
def two_sum(nums, target):
seen = {} # value → index
for i, n in enumerate(nums):
complement = target - n
if complement in seen:
return [seen[complement], i]
seen[n] = i
return []This pattern generalizes. You can use it for finding pairs with a given difference, detecting duplicates within a distance, or checking if elements form a valid sequence. The structure stays the same: initialize an empty map, iterate once, check then store.
Pattern to memorise: seen = {}; for each x, check if target-x in seen, then store seen[x] = i. This pattern solves dozens of interview problems.
Use arrays when you need ordered access or iteration. Use hash maps when you need fast lookups or when you're building a frequency table. Often you'll use both: iterate through an array, populate a hash map, then query it.
The trade-off is simple. Arrays use less memory and preserve order. Hash maps use more memory but give you constant-time operations that arrays don't.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.