Hourly DSABlog

Hash Map vs. Hash Set: Which Does Your Problem Need?

By Hourly DSA · · 4 min read

Use a hash set when you only need to know whether something is present. Use a hash map when each key needs associated information, such as a count or an index. The choice starts with the question your algorithm must answer about values it has already seen.

Both structures can help replace repeated searches through an array. The useful distinction is what you need to remember after finding a match.

A set answers a membership question

Suppose you need to determine whether this array contains a duplicate:

[8, 3, 6, 3]

Scan from left to right, keeping a set of previously seen values. After the first three positions, the set contains 8, 3, and 6. The next value is 3, which is already present, so the answer is true.

You don't need to know how often 3 appears or where its first occurrence was. Membership is enough.

The order of operations matters:

seen = empty set
for value in values:
    if value is in seen:
        return true
    add value to seen
return false

If you insert before checking, every value appears to be a duplicate of itself. A one-element array exposes that mistake immediately.

A map remembers information about each key

Now change the question: how many times does each value appear?

For the same array, the answer is:

8 → 1
3 → 2
6 → 1

A set cannot represent those counts by itself. A map can associate each value with its frequency. On each visit, increase the stored count, treating a missing key as count zero.

Other questions call for different values in the map. To return the first occurrence of each number, store its earliest index and keep that index when the number repeats. To return its latest occurrence, overwrite the index on every visit.

“Use a hash map” is only half an explanation. Finish it with what the key and value mean.

Two Sum: the requested output changes the structure

Consider an unsorted array and a target of 11:

[4, 8, 3, 7]

For each value, ask whether its complement has appeared earlier. At 4, look for 7. At 8, look for 3. At 3, look for 8, which is already present.

If the question only asks whether a pair exists, a set is enough. If it asks for the pair's indices, use a map from value to index. When you reach 3 at index 2, the map tells you that 8 appeared at index 1.

Again, check before inserting. With [5] and target 10, there is no pair of distinct positions. Inserting 5 first would let that position match itself. With [5, 5], the second position can correctly match the first.

If the input is already sorted, two pointers offer another approach with constant extra space. Input order and output requirements both affect the decision.

What are the time and space costs?

With a well-behaved hash table, lookups are expected O(1), and insertions are expected amortized O(1), accounting for occasional resizing. Scanning n fixed-size values therefore takes expected O(n) time. Storing up to n distinct keys takes O(n) extra space.

These are conditional bounds. Collisions, resizing, and the cost of hashing or comparing a key matter. Hashing a long string can depend on its length. Princeton's hash-table explanation describes the hashing assumptions and collision handling behind these operations.

For an interview answer, state the expected bound and the key assumptions. Don't describe all hash-table operations as unconditionally constant time.

Choose the smallest useful record

What you need to askWhat to store
Have I seen this value?Set of values
How often has it appeared?Map from value to count
Where did it first appear?Map from value to earliest index
Which previous value completes this pair?Set, or a map if indices are needed

A set also removes multiplicity: inserting the same value twice still leaves one distinct entry. If duplicates affect the answer, make sure that loss of information is intentional.

Try one change to the problem

For [2, 7, 2, 7, 7], which structure would you use to find the most frequent value?

A frequency map. It records 2 → 2 and 7 → 3, so 7 wins. A set would contain only {2, 7} and could not tell you which occurred more often.

Practice naming the information you need before naming the data structure. Hourly DSA gives you short opportunities to work on that decision, and our guide to reviewing DSA mistakes helps turn a wrong choice into a useful follow-up question.

Explore what changes when you need prefix matching instead of exact membership.

Tries Explained: Follow a Prefix One Character at a Time →