Hash Sets

Why Use HashSet Instead of List in C#?

Use a hash set when you need fast membership checks and unique items; use a list when order, indexing or duplicates matter. Compare C# and C++ examples.

On this page
  1. Short answer
  2. What a list does well
  3. What a hash set does well
  4. The C++ pair
  5. A quick check
  6. When a list is still the right choice
  7. Summary

Short answer

Use a HashSet<T> in C# when you need fast membership checks and unique items. In C++, that role belongs to std::unordered_set. Use a List<T> in C# when order, access by position, or duplicate values matter. The C++ equivalent of that list is std::vector.

What a list does well

A List<T> keeps items in the order you add them, lets you read an item by its position, and allows duplicates. Those are useful features. To check whether a value is in a list, though, the program usually has to look at the items one at a time until it finds a match or reaches the end. With List<T>.Contains, the time grows with the number of items.

What a hash set does well

A HashSet<T> checks membership by hashing the value, so a check takes constant time on average. It also keeps only one copy of each value. Calling Add with a value that is already present returns false and changes nothing.

The trade-off is that a hash set has no positions and no guaranteed order. You cannot write colors[2], because a HashSet<T> has no index.

Here is the difference in C#:

using System.Collections.Generic;

var names = new List<string> { "Ana", "Ana" };              // Count is 2
var uniqueNames = new HashSet<string> { "Ana", "Ana" };     // Count is 1

var listItems = new List<int>();
var setItems = new HashSet<int>();

for (int i = 0; i < 10000; i++)
{
    listItems.Add(i);
    setItems.Add(i);
}

bool inList = listItems.Contains(9999);  // scans the list
bool inSet = setItems.Contains(9999);    // hashes the value

The C++ pair

In C++, the list-style container is std::vector, and the hash set is std::unordered_set. Here they are side by side:

#include <algorithm>
#include <iostream>
#include <unordered_set>
#include <vector>

int main() {
    std::vector<int> items;
    std::unordered_set<int> lookup;

    for (int i = 0; i < 10000; ++i) {
        items.push_back(i);
        lookup.insert(i);
    }

    // Scans the vector until it finds 9999 or reaches the end
    bool in_vector = std::find(items.begin(), items.end(), 9999) != items.end();

    // Hashes 9999 and checks the matching bucket
    bool in_set = lookup.find(9999) != lookup.end();

    std::cout << std::boolalpha << in_vector << " " << in_set << "\n";  // true true
    return 0;
}

The vector keeps duplicates and positions, while the unordered set keeps only one copy of each value.

A quick check

If you are unsure which one to use, measure with data close to your real size. Small collections often run fast with either one. The difference usually matters when the collection is large and you check membership often.

When a list is still the right choice

  • The collection is small, so scanning it costs little.
  • You need the items in a specific order, or you need to read them by position.
  • You need duplicates, such as a record of repeated events.

If you are coming from C# or Java, the guide on arraylist in c shows how a dynamic array maps to std::vector. The difference between list-style and array-style storage is covered in list vs array c.

Summary

Need Better choice
Check membership many times Hash set
Keep only unique items Hash set
Keep order or access items by position List or vector
Keep duplicate values List or vector

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 →

Static Classes in C++

C++ has no static class keyword. Here is how static members, utility classes and namespaces cover the same job, with short code examples for each.

Static Classes 5 min read

Concurrency in C++

Learn concurrency in C++ with std::thread, mutexes, atomics and futures. Covers data races, deadlocks and common mistakes, with short compilable examples.

Concurrency 5 min read