Hash Sets

Which Is Faster, HashSet or HashMap?

Neither HashSet nor HashMap is faster in general. A set stores only keys and a map stores key-value pairs, so the right pick depends on your data.

On this page
  1. Short answer
  2. What each one stores
  3. What speed depends on
  4. A quick comparison in code
  5. How to measure it yourself
  6. How to choose
  7. Summary

Short answer

Neither is faster in general, because they do different jobs. A set stores only keys. A map stores a key together with a value. Both usually find an item in average constant time, so the right choice depends on what you need to store and how you use it.

“HashMap” is the Java name for this kind of container. In C++ the equivalent is std::unordered_map, and C# uses Dictionary<TKey, TValue>.

What each one stores

  • std::unordered_set<T> stores keys only. Each element is a value you check for, such as a username.
  • std::unordered_map<K, V> stores a key with a value attached, such as a username with a score.
std::unordered_set<std::string> usernames;
usernames.insert("alice");  // only the key

std::unordered_map<std::string, int> scores;
scores["alice"] = 42;       // a key with a value

The naming differs across languages, but the ideas line up:

Language Set of keys Key-value map
Java HashSet HashMap
C++ std::unordered_set std::unordered_map
C# HashSet<T> Dictionary<TKey, TValue>

The key-value pattern is covered in the guide on dictionary in c.

What speed depends on

Speed in a hash container depends on several factors, and most of them apply to both:

  • The hash function. A poor hash function puts many items in the same bucket, and lookups slow down.
  • The key. Hashing and comparing a long string costs more than hashing a small integer.
  • The load factor. This is how full the container is compared with its number of buckets. When it grows past a limit, the container rehashes, which takes time.
  • Memory layout. Hash containers often store their elements in separate nodes, which can cause cache misses.

A quick comparison in code

The same membership check looks like this in both containers:

#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>

int main() {
    std::unordered_set<int> ids = {101, 202, 303};
    std::unordered_map<int, std::string> names = {{101, "Ada"}, {202, "Lin"}, {303, "Sam"}};

    std::cout << ids.count(202) << "\n";    // 1: membership only
    std::cout << names.count(202) << "\n";  // 1: the key is present
    std::cout << names.at(202) << "\n";     // Lin: the value stored for that key
    return 0;
}

Both membership checks take constant time on average. The map can also return the value stored for a key with at(), which the set has no equivalent for. The set answers only whether a value is present. Because a set does not store a value for each element, it may also use less memory for the same number of keys.

How to measure it yourself

If speed matters for your program, measure with your own data:

  • Time the operations with std::chrono::steady_clock.
  • Use data sizes close to what your program actually handles.
  • Compile with optimizations turned on, such as -O2 with GCC or Clang. Debug builds can be slow and misleading.
  • Run each test several times and compare the results.

Your results will depend on your machine, compiler and data.

How to choose

  • Use a set when you only need to know whether an item is present, or when you want to remove duplicates.
  • Use a map when each key needs a value, such as a count, a name or a setting.
  • If you need the keys in sorted order, the ordered std::set is the third option. The guide on sets in c explains how it differs from the unordered versions.

Summary

Neither HashSet nor HashMap is faster in general. Choose the set for membership checks and the map for key-value lookups. If speed matters, time both with data that looks like your real workload.

The complete guide to hash sets

Hash Sets in C++ With std::unordered_set
Read the guide

Keep exploring

Guides from other topics

All topics →

How to Use cout in C++

Learn how to use cout in C++ to print text, numbers and variables, add new lines, and format output, with short complete examples you can compile and run.

cout 4 min read