C++ Unordered_set::count() Function



The C++std::unordered_set::count() function is used to count the number of particular elements or the specified key k in the unordered_set container. It does not accept any duplicate value , so if the key is present in the set it returns 1, otherwise 0.

The unordered_set is an associative container that contains a set of unique objects of type key. Every operation like insertion, search, and removal in an unordered_set has constant-time complexity.

Syntax

Following is the syntax of std::unordered_set::count() function.

size_type count ( const key_type& k ) const;

Parameters

  • k − It indicates the search element.

Return Value

It returns 1 if an element with a value equivalent to k is found in unordered_set container, otherwise zero.

Example 1

Let's look at the following example, where we are going to demonstrate the usage of count() function.

#include <iostream>
#include <string>
#include <unordered_set>

int main () {
   std::unordered_set<std::string> myset = { "sairam", "krishna", "prasad" };
   for (auto& x: {"tutorialspoint","sairam","krishna","t-shirt"}) {
      if (myset.count(x)>0)
         std::cout << "myset has " << x << std::endl;
      else
         std::cout << "myset has no " << x << std::endl;
   }
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

myset has no tutorialspoint
myset has sairam
myset has krishna
myset has no t-shirt

Example 2

Consider the following example, where we are going to use the unordered_set with integer type and going to check whether the element exists or not.

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

int main () {
   unordered_set<int> myUset = { 10, 20, 30, 50, 60 };
   cout<<"following is the element of the unordered_set!"<<endl;
   for(auto it:myUset){
      cout<<it<<endl;
   }
   cout<<"is 10 present in the set: "<<myUset.count(20)<<endl;
   cout<<"is 70 present in the set: "<<myUset.count(70)<<endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

following is the element of the unordered_set!
60
50
30
20
10
is 10 present in the set: 1
is 70 present in the set: 0

Example 3

In the following example, we are going to use the unordered_set with type string and applying the count() function to check whether the element exists or not and displaying the if statement based on the conditions.

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

int main () {
   unordered_set<string> myUset = { "tutorialspoint", "India", "PVT", "LTD" };
   cout<<"following is the element of the unordered_set!"<<endl;
   for(auto it:myUset){
      cout<<it<<" ";
   }
   cout<<endl;
   
   if(myUset.count("tutorialspoint") == 1)
      cout<<"tutorialspoint is present in the set: "<<endl;
   else
      cout<<"tutorialspoint is not present in the set: "<<endl;
   return 0;
}

Output

Following is the output of the above code −

following is the element of the unordered_set!
LTD PVT India tutorialspoint 
tutorialspoint is present in the set: 

Example 4

Following the another example of using the count() function with unordered_set of string type and checking the whether the element exists or not.

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

bool isPresent(string s, char ch) {
   unordered_set<char> set;

   for(int i=0; i<s.size(); i++){
      set.insert(s[i]);
   }
   if(set.count(ch) == 1){
      cout<<"char "<<ch<<" is present so it will print: ";
      return true;
   }
   
   cout<<"char "<<ch<<" is not present so it will print: ";
   return false;
}
int main() {
   cout << isPresent("tutorialspoint", 't') << '\n';
   return 0;
}

Output

Output of the above code is as follows −

char t is present so it will print: 1