diff --git a/content/cpp/concepts/unordered-set/terms/load-factor/load-factor.md b/content/cpp/concepts/unordered-set/terms/load-factor/load-factor.md new file mode 100644 index 00000000000..8c94e367855 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/load-factor/load-factor.md @@ -0,0 +1,96 @@ +--- +Title: 'load_factor()' +Description: 'Returns the current load factor in an unordered set.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Containers' + - 'Methods' + - 'Sets' + - 'STL' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +In C++, the **`load_factor()`** method returns the current load factor of an `unordered_set`. The load factor is defined as the average number of elements per bucket and is calculated as: + +``` +number of elements รท number of buckets +``` + +This value helps evaluate how efficiently the hash table is using its buckets and can indicate when rehashing may occur. + +## Syntax + +```pseudo +unordered_set_name.load_factor(); +``` + +**Parameters:** + +This method takes no parameters. + +**Return value:** + +Returns a floating-point value of type `float` representing the current load factor of the container. + +## Example 1: Retrieving the Load Factor + +This example shows how to retrieve the load factor after inserting elements into an `unordered_set`: + +```cpp +#include +#include + +int main() { + std::unordered_set numbers = {1, 2, 3, 4, 5}; + + std::cout << "Load factor: " << numbers.load_factor() << std::endl; + + return 0; +} +``` + +The output of this code is: + +```shell +Load factor: 1 +``` + +## Codebyte Example: Observing Load Factor Changes During Insertions + +This example demonstrates how the load factor changes as elements are added to an `unordered_set`: + +```codebyte/cpp +#include +#include + +int main() { + std::unordered_set values; + + for (int i = 1; i <= 20; i++) { + values.insert(i); + std::cout << "After inserting " << i + << " elements, load factor: " + << values.load_factor() << std::endl; + } + + return 0; +} +``` + +## Frequently Asked Questions + +### 1. What is the load factor of `unordered_map`? + +The load factor of an `unordered_map` is the average number of elements per bucket, calculated as the number of elements divided by the number of buckets. + +### 2. What does `unordered_set` do in C++? + +An `unordered_set` stores unique elements in no particular order using a hash table, allowing fast average-time insertion, deletion, and lookup. + +### 3. How to implement `unordered_set` in C++? + +Include the `` header, create an `unordered_set` object, and insert elements using insert() or initializer lists while relying on hashing for storage and access.