C++ std::unordered_set count() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::unordered_set

Definition

In C++, std::unordered_set::count() returns the number of times a value is stored. A std::unordered_set never holds the same value twice, so the answer is always 0 or 1.

That makes it a simple “does this value exist?” test. It does not add the value. Include <unordered_set> to use it.

This is a member of std::unordered_set<T>, a hash table of unique values. Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.

Syntax

Pass the value you are looking for.

C++
if (s.count("Raj") == 1) {
    // value exists
}
bool has = s.count("Zoe") > 0;
CallWhat it does
s.count(value)Returns 1 if the value is in the unordered_set, and 0 if it is not.

count() only says whether the value exists. To also get an iterator to it, use find(). Since C++20, contains() expresses the same test more clearly. A std::multiset can return larger numbers, but a std::unordered_set cannot.

Library declarations
C++
size_type count(const key_type& value) const;
template<class K> size_type count(const K& x) const;   // transparent hash/eq

Parameters

ParameterDescription
valueThe value to look for.

Return Value

Return ValueDescription
1The value is in the unordered_set.
0The value is not in the unordered_set.

C++ Examples

Example 1 — count() Returns 0 or 1

Ask for one value that exists and one that does not.

C++
#include <iostream>
#include <unordered_set>
#include <string>

int main() {
    std::unordered_set<std::string> names = {"Mia", "Raj"};

    std::cout << "Raj: " << names.count("Raj") << '\n';
    std::cout << "Zoe: " << names.count("Zoe") << '\n';
    std::cout << "size: " << names.size() << '\n';
    return 0;
}

How It Works

  1. count("Raj") searches the sorted values and finds one match, so the result is 1.
  2. count("Zoe") finds none, so the result is 0. Because values are unique, it can never be 2 or more.
  3. The size is still 2. Asking with count() does not add "Zoe".

Example 2 — count() in a Condition

Add a color to an unordered_set only the first time you see it.

C++
#include <iostream>
#include <unordered_set>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> input = {"red", "blue", "red", "green", "blue"};
    std::unordered_set<std::string> seen;

    for (const std::string& color : input) {
        if (seen.count(color) == 0) {
            seen.insert(color);
            std::cout << "new: " << color << '\n';
        }
    }

    std::cout << "unique: " << seen.size() << '\n';
    return 0;
}

How It Works

  1. The loop reads each color. count(color) == 0 is true only when the color has never been stored.
  2. For a new color, insert() stores it and a message prints. The second red and the second blue find a count of 1, so they are skipped.
  3. The unordered_set ends with three unique colors. The printed lines show the order of first discovery, not hash-bucket order.

Example 3 — count() Added Up

Check how many of several wanted values are present by adding the results.

C++
#include <iostream>
#include <unordered_set>
#include <string>
#include <vector>

int main() {
    std::unordered_set<std::string> menu = {"tea", "coffee", "juice"};
    std::vector<std::string> wanted = {"tea", "cake", "juice", "soup"};

    int available = 0;
    for (const std::string& item : wanted) {
        available += static_cast<int>(menu.count(item));
    }

    std::cout << available << " of " << wanted.size() << " items are on the menu\n";
    return 0;
}

How It Works

  1. menu has three drinks. wanted lists four items, two of which are not on the menu.
  2. Each menu.count(item) is 1 for tea and juice, and 0 for cake and soup. Adding them gives 2.
  3. This only works as a sum because the answer is always 0 or 1 for a std::unordered_set.

Frequently Asked Questions

The number of times the value is stored. For a std::unordered_set that is only 0 or 1, because values are unique.
No. It only looks. An unordered_set has no operator[], so you never insert by accident with a lookup.
count() only says whether the value exists. find() returns an iterator, so you can also use or erase the value without a second search.
They give the same answer for an unordered_set, but contains() (C++20) returns a bool and reads more clearly. count() works in every C++ version.
It is a short exists check, and the same function name works on containers where larger counts are possible, such as multiset.

Next: emplace()

Keep going through the std::unordered_set functions. Build a value directly inside the unordered_set (C++11).

emplace() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful