Unordered Sets

Unordered Sets

 
In C++, an unordered_set is a container class template from the Standard Template Library (STL) that stores unique elements in an unordered manner.
An unordered_set stores only unique elements and does not allow duplicates. The position of each element in the container is determined by a hash function. This means that the elements are not sorted in any specific order.
Unordered_sets are implemented using hash tables, which provide efficient insertion, deletion, and searching of elements. They have an average time complexity of O(1) for these operations. However, their worst-case time complexity can be as bad as O(n) in certain situations.
Unordered_sets are useful in situations where you do not need the elements to be stored in a specific order, but still require fast insertion, deletion, and searching of elements. For example, you could use an unordered_set to store a list of unique employee IDs in a company. The unordered_set would quickly check whether a given ID already exists in the container.
You can also use an unordered_set to implement various algorithms such as finding the intersection, union, or difference of two sets. The unordered_set container provides a set of member functions to perform these operations. However, because the elements are not sorted, the results of these operations may not be in any specific order.
 
 
#include <iostream> #include <unordered_set> using namespace std; int main() { // Create an unordered set of integer values unordered_set<int> myUnorderedSet; // Insert elements in the unordered set myUnorderedSet.insert(10); myUnorderedSet.insert(20); myUnorderedSet.insert(30); myUnorderedSet.insert(40); myUnorderedSet.insert(50); // Print the elements in the unordered set cout << "Current unordered set elements: " << endl; for (auto it = myUnorderedSet.begin(); it != myUnorderedSet.end(); it++) { cout << *it << endl; } cout << endl; // Search for an element in the unordered set auto it = myUnorderedSet.find(30); if (it != myUnorderedSet.end()) { cout << "Element found in the unordered set: " << *it << endl; } else { cout << "Element not found in the unordered set" << endl; } // Erase an element from the unordered set myUnorderedSet.erase(40); // Print the modified unordered set cout << "Modified unordered set after erasing 40: " << endl; for (auto it = myUnorderedSet.begin(); it != myUnorderedSet.end(); it++) { cout << *it << endl; } cout << endl; // Clear all of the elements of the unordered set myUnorderedSet.clear(); // Check if the unordered set is empty if (myUnorderedSet.empty()) { cout << "Unordered set is empty" << endl; } else { cout << "Unordered set is not empty" << endl; } return 0; }
 
This implementation creates an empty unordered set of integer values myUnorderedSet. It then adds elements to the unordered set using the insert() function. The elements are printed to the console using a for loop and the begin() and end() functions of the unordered set.
The find() function is used to search for an element in the unordered set. If the element is found, its value is printed to the console.
The erase() function removes an element from the unordered set. The modified unordered set is printed to the console.
The clear() function deletes all of the elements of the unordered set, and the empty() function checks if the unordered set is empty. Finally, the program returns 0.
 
 
PRACTICE PROBLEMS :
  1. Minimum Absolute Sum Difference