GeeksforGeeks App
Open App
Browser
Continue

# unordered_set count() function in C++ STL

The unordered_set::count() function is a built-in function in C++ STL which is used to count occurrences of a particular element in an unordered_set container. As the unordered_set container does not allows to store duplicate elements so this function is generally used to check if an element is present in the container or not. The function returns 1 if the element is present in the container otherwise it returns 0.
Syntax

`unordered_set_name.count(element)`

Parameter: This function accepts a single parameter element. This parameter represents the element which is needed to be checked if it is present in the container or not.
Return Value: This function returns 1 if the element is present in the container otherwise it returns 0.
Time Complexity: Time Complexity for unordered_set::count() method is O(1) in average cases, but in worst case, time complexity can be O(N) i.e. Linear, where N is size of container.

Below programs illustrate the unordered_set::count() function:

Program 1

## CPP

 `// CPP program to illustrate the``// unordered_set::count() function` `#include ``#include ` `using` `namespace` `std;` `int` `main()``{` `    ``unordered_set<``int``> sampleSet;` `    ``// Inserting elements``    ``sampleSet.insert(5);``    ``sampleSet.insert(10);``    ``sampleSet.insert(15);``    ``sampleSet.insert(20);``    ``sampleSet.insert(25);` `    ``// displaying all elements of sampleSet``    ``cout << ``"sampleSet contains: "``;``    ``for` `(``auto` `itr = sampleSet.begin(); itr != sampleSet.end(); itr++) {``        ``cout << *itr << ``" "``;``    ``}` `    ``// checking if element 20 is present in the set``    ``if` `(sampleSet.count(20) == 1) {``        ``cout << ``"\nElement 20 is present in the set"``;``    ``}``    ``else` `{``        ``cout << ``"\nElement 20 is not present in the set"``;``    ``}` `    ``return` `0;``}`

Output:

```sampleSet contains: 25 5 10 15 20
Element 20 is present in the set```

Program 2

## CPP

 `// C++ program to illustrate the``// unordered_set::count() function` `#include ``#include ` `using` `namespace` `std;` `int` `main()``{` `    ``unordered_set sampleSet;` `    ``// Inserting elements``    ``sampleSet.insert(``"Welcome"``);``    ``sampleSet.insert(``"To"``);``    ``sampleSet.insert(``"GeeksforGeeks"``);``    ``sampleSet.insert(``"Computer Science Portal"``);``    ``sampleSet.insert(``"For Geeks"``);` `    ``// displaying all elements of sampleSet``    ``cout << ``"sampleSet contains: "``;``    ``for` `(``auto` `itr = sampleSet.begin(); itr != sampleSet.end(); itr++) {``        ``cout << *itr << ``" "``;``    ``}` `    ``// checking if element GeeksforGeeks is``    ``// present in the set``    ``if` `(sampleSet.count(``"GeeksforGeeks"``) == 1) {``        ``cout << ``"\nGeeksforGeeks is present in the set"``;``    ``}``    ``else` `{``        ``cout << ``"\nGeeksforGeeks is not present in the set"``;``    ``}` `    ``return` `0;``}`

Output:

```sampleSet contains: Welcome To GeeksforGeeks For Geeks Computer Science Portal
GeeksforGeeks is present in the set```

My Personal Notes arrow_drop_up