Skip to content
Related Articles

Related Articles

set count() function in C++ STL
  • Difficulty Level : Easy
  • Last Updated : 10 Jul, 2018

The set::count() is a built-in function in C++ STL which returns the number of times an element occurs in the set. It can only return 1 or 0 as the set container contains unique elements only.

Syntax:

set_name.count(element) 

Parameters: The function accepts one mandatory parameter element which specifies the element whose count is to be returned.

Return Value: The function returns 1 or 0 as the set contains unique elements only. It returns 1 if the value is present in the set container. It returns 0 if it is not present in the container.

Below is the illustration of the above function.




// CPP program to demonstrate the
// set::count() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
  
    int arr[] = { 14, 12, 15, 11, 10 };
  
    // initializes the set from an array
    set<int> s(arr, arr + 5);
  
    // check if 11 is present or not
    if (s.count(11))
        cout << "11 is present in the set\n";
    else
        cout << "11 is not present in the set\n";
  
    // checks if 18 is present or not
    if (s.count(18))
        cout << "18 is present in the set\n";
    else
        cout << "18 is not present in the set\n";
  
    return 0;
}
Output:
11 is present in the set
18 is not present in the set
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :