Short answer
A HashSet is a collection of unique items. It uses a hash function to work out where each item is stored, so checking whether an item is present is usually quick. The name comes from C# and Java. In C++, the same idea is called std::unordered_set.
The idea in one sentence
A hash set stores a group of distinct values, does not keep them in any particular order, and can answer “is this value here?” quickly, even when it holds many values.
How hashing finds an item
A hash function takes a value and turns it into a number, often called a hash. The container uses that number to choose a bucket, which is a slot in an internal array. To check for a value, the container hashes it, goes to the matching bucket, and compares the value only with the few items stored there. It does not have to look at every item.
Two different values can produce the same hash. This is called a collision. The container handles it internally, usually by storing several values in the same bucket. A good hash function spreads values across the buckets so that collisions stay uncommon.
A HashSet in C++
In C++, the type is std::unordered_set, declared in <unordered_set>. Here is a small example:
#include <iostream>
#include <string>
#include <unordered_set>
int main() {
std::unordered_set<std::string> colors;
colors.insert("red");
colors.insert("green");
colors.insert("red"); // not added again
std::cout << "Count: " << colors.size() << "\n"; // prints 2
if (colors.find("green") != colors.end()) {
std::cout << "green is in the set\n";
}
return 0;
}
The second "red" does not change the set, so the size is 2. The check for "green" uses find(), which returns an iterator. Comparing that iterator with end() tells you whether the value was found.
How it differs from a list or a sorted set
Compared with std::vector:
- A vector keeps items in the order you add them, lets you access items by position, and allows duplicates.
- A hash set has no positions and no duplicates. Checking for an item is usually faster on a large collection.
Compared with std::set:
- A
std::setkeeps items in sorted order. It is usually built on a balanced tree, so lookups take time that grows with the logarithm of its size. - A
std::unordered_sethas no sorted order. Its lookups take constant time on average.
For sorted data, read about sets in c. For a full walkthrough of the C++ type, read about hashset in c.
Limits to keep in mind
- There is no guaranteed order, so the order of a loop can change.
- The element type needs a hash and
==. Built-in types such asintandstd::stringalready have them. - Speed depends on the hash function and on how full the set gets. As the set grows, it may rehash into more buckets, and that takes time.
- A hash set is not always the fastest choice. For a handful of items, a plain scan of a vector can be just as quick.
Summary
A HashSet is a set of unique items found through hashing. In C++, it is std::unordered_set. It suits programs that need fast membership checks and do not need the items in a particular order.