VOOZH about

URL: https://www.geeksforgeeks.org/cpp/set-count-function-in-c-stl/

⇱ set::count() Function in C++ STL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

set::count() Function in C++ STL

Last Updated : 11 Jul, 2025

The std::set::count() is a built-in function in C++ STL which is used to count the number of times an element occurs in the set container. std::set container stores unique elements, so it can only return 1 or 0. Therefore, it is only used for checking if the element exists in the set or not.

Example


Output
1
0

set::count() Syntax

s.count(val);

where, s is the name of std::set container.

Parameters

  • val: Value whose count is to be determined.

Return Value

  • Returns 1, if the value is present in the set.
  • Returns 0, if the value is not present in the set.

Complexity Analysis

The std::set container is generally implemented by a self-balancing binary search tree. The set::count() function searches for the given value in the set. So, search operation in self-balancing BST,

Time Complexity: O(log n), where n is the number of elements in the set.
Auxiliary Space: O(1)

More Examples of set::count()

The following examples illustrates the use of set::count() function:

Example 1: Checking Element Existence Using set::count()


Output
Not exists

Example 2: Comparing set::count() with multiset::count()


Output
Set count of 2: 1
Multiset count of 2: 2
Comment