A hash set stores keys only; a hash map stores key-value pairs. Both use the same underlying mechanism — pick the one that matches what you actually need.
Hash set
Operations: insert, delete, contains — all expected. Use for membership queries, deduplication, finding intersections.
Hash map
Adds get/put for the value side. Use whenever you need to associate metadata with the key — counts, indices, the most recent value, anything.
Common transformations
- Want to dedupe an array? Convert to a set, then back to a list.
- Want to count occurrences? Hash map of value to count. Some languages provide a Counter / multiset class that wraps this.
- Want to track first/last seen index? Hash map of value to index.
- Want to find common elements between arrays? Intersect their sets.
Multi-set (bag)
Some problems need a "set" that allows duplicates. A hash map from element to count is the standard implementation. Use it when you need to subtract elements (as in sliding-window problems with character counts).
Ordered variants
Tree map (Java's TreeMap, ++'s std::map) stores keys in sorted order — operations instead of but supports range queries and ordered iteration. Reach for these when you need to find "smallest key larger than X" or "all keys in range [a, b]."