diff --git a/content/cpp/concepts/unordered-set/terms/count/count.md b/content/cpp/concepts/unordered-set/terms/count/count.md new file mode 100644 index 00000000000..839724329d5 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/count/count.md @@ -0,0 +1,108 @@ +--- +Title: 'count()' +Description: 'Returns the number of elements with a specific key in an unordered_set.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Methods' + - 'Sets' + - 'STL' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`count()`** method checks whether a given key exists in a `std::unordered_set`. Since this container stores only unique elements, `count()` will always return either: + +1. `1`: If the element is found in the set. +2. `0`: If the element is not found in the set. + +This method is commonly used as a fast, O(1) average time complexity way to check for element existence. + +## Syntax + +```pseudo +unordered_set_name.count(key); +``` + +**Parameters:** + +- `key` (const Key&): The value of the element to search for. Must be of the same type as the elements stored in the `unordered_set`. + +**Return value:** + +Returns an integer. `1` if the element exists, `0` otherwise. + +## Example + +This example demonstrates using `count()` to check for the presence of elements within a set of strings: + +```cpp +#include +#include +#include + +int main() { + std::unordered_set inventory = { + "Sword", + "Shield", + "Potion" + }; + + std::cout << "Inventory contains:\n"; + for (const auto& item : inventory) { + std::cout << "- " << item << "\n"; + } + + // 1. Check for an existing element + if (inventory.count("Sword")) { + std::cout << "\n'Sword' is present (Count: " << inventory.count("Sword") << ").\n"; + } + + // 2. Check for a missing element + if (inventory.count("Axe") == 0) { + std::cout << "'Axe' is not present (Count: " << inventory.count("Axe") << ").\n"; + } + + return 0; +} +``` + +The output of the code is: + +```shell +Inventory contains: +- Potion +- Shield +- Sword + +'Sword' is present (Count: 1). +'Axe' is not present (Count: 0). +``` + +## Codebyte Example + +Use the Codebyte below to check for the presence of an item in a set of integers: + +```codebyte/cpp +#include +#include + +int main() { + std::unordered_set unique_ids = {101, 205, 330}; + + int search_key = 205; + int missing_key = 400; + + // Check the count for the element 205 + std::cout << "Count for " << search_key << ": " + << unique_ids.count(search_key) << "\n"; + + // Check the count for the element 400 + std::cout << "Count for " << missing_key << ": " + << unique_ids.count(missing_key) << "\n"; + + return 0; +} +```