Step 01 Beginner Problems — Concept Basics
Basic Hashing
Hashing is the idea that powers the most useful beginner technique in all of DSA: counting frequencies and answering 'have I seen this?' in O(1). Instead of searching a list linearly for every query, you store values in a structure keyed by the value itself. This lesson builds hashing from a simple frequency array up to the hash map, explains why it is fast on average, and covers the problems this module asks about it.
The core idea: trade memory for speed
Suppose you must answer, for many queries, 'how many times does x occur in this array?' Scanning the array per query costs O(n) each. Instead, precompute a frequency table once — one pass, O(n) — and then every query is a single lookup, O(1). That is hashing's bargain: spend memory on a table, buy back time on every lookup.
The simplest table is a frequency array: if values are known to lie in a small range (say 0–100, or lowercase letters), freq[value] += 1 indexes directly by the value. Two lines, no library, unbeatable speed. The hash map of the next section generalises this to any values at all.
The hash function and the hash map
A hash map stores keys of any type by running each key through a hash function that converts it to a bucket number. Insert, lookup and delete then touch exactly one bucket — average O(1). When two keys land in the same bucket (a collision), the bucket holds both and the map checks equality within it; well-designed hash functions spread keys so evenly that buckets stay tiny.
The honest complexity statement: average O(1), worst case O(n) if many keys collide — which is why interviews ask 'average or worst?' and why ordered maps (trees, O(log n) guaranteed) exist as the alternative. In Python the hash map is dict; in C++, unordered_map; in Java, HashMap.
def frequencies(nums):
freq = {}
for x in nums:
freq[x] = freq.get(x, 0) + 1
return freq
f = frequencies([1, 2, 2, 3, 3, 3])
print(max(f, key=f.get)) # 3 -> highest occurring elementCharacter hashing and the anagram connection
For strings over a fixed small alphabet, a plain array beats a map: counts = [0] * 26, and each character c increments counts[ord(c) - ord('a')]. Two strings are anagrams exactly when their count arrays are equal — no sorting needed, O(n) instead of O(n log n). The same trick with a 256-slot array covers all ASCII.
Pattern recognition: any problem phrased as 'same multiset of characters/elements' — anagrams, permutations-in-string, ransom notes — is a frequency-equality problem. Build both tables (or build one and decrement with the other) and compare.
Choosing: frequency array, hash map, or set
Frequency array when values are small-range integers or characters and you know the range in advance — fastest and simplest, but impossible for huge or negative ranges without offsetting. Hash map when values are arbitrary (large numbers, strings) or the range is unknown — the general answer. Set when you only need presence, not counts: duplicates removal, 'have I seen this value' (which powers the classic two-sum pattern later), and membership tests inside loops.
One warning: hashing needs the values themselves as keys. If you must count things that are unhashable in your language (lists in Python), convert them first (to tuples) or count a derived key.
Common mistakes
Indexing a frequency array with a value outside its range (negative numbers crash it — offset or use a map). Building a frequency table and then scanning the ORIGINAL array to find the max-frequency element when scanning the table is cheaper and cleaner. Forgetting that map iteration order is unspecified in most languages, so tie-breaking ('first element with max frequency') must be handled deliberately, usually by scanning the original array once the max frequency is known. And solving counting problems with nested loops — the O(n^2) smell that hashing exists to eliminate.
Key points
- Hashing trades memory for time: build a frequency table once in O(n), answer every lookup in O(1).
- Frequency array when the value range is small and known; hash map for arbitrary values; set for presence-only.
- Hash map operations are average O(1) (collisions make the worst case O(n)) — state which one you mean.
- Anagram-type problems are frequency-equality problems: compare count tables, no sorting needed.
- Character counting with a 26-slot array beats a map when the alphabet is fixed.
- Tie-breaking on max frequency needs a deliberate rule — map iteration order is not your friend.