Start learning free

LESSON 351 OF 667 · HASH TABLE

Hash Sets in C++

Lesson

Hash Sets are collections of unique items i.e no element can be repeated.

Hash Sets were designed to give us a way of adding and looking for unique values in a collection in a quick manner.

The values in a Hash Set can be either simple primitives like strings or integers as well as more complex object types like object literals or arrays.

In C++, the Hash Set is implemented as an unordered_set object.


Creation:

To create a new empty unordered_set that holds strings, you use the following syntax:

unordered_set<string> mySet;

Creating an empty Set takes O(1) time.


Constructing from iterable

The Set constructor also accepts an optional iterable object. If you pass an iterable object to the Set constructor, all the unique elements of the object will be added to the new set:

vector<string> elements = {"b", "c", "c", "d"};
unordered_set<string> mySet(elements.begin(), elements.end());

// mySet contains {"b", "c", "d"}

This takes O(n) time, where n is the number of elements in the iterable object.


Adding elements:

To add an element to the set, you use the insert() method:

unordered_set<string> mySet({"b", "c", "c", "d"});

// Adding new elements
mySet.insert("x");

// Adding an existing element
mySet.insert("d"); // nothing happens

// mySet contains {"b", "c", "d", "x"}

The Set first checks if that element already exists and if so, it does nothing and also doesn't raise an error.

Adding an element to a Set takes O(1) time.


Checking if a value exists:

To check if a Set has a specific element, you use the count() method which returns 1 if the set contains the element, otherwise, it returns 0.

unordered_set<string> mySet({"b", "c", "c", "d"});

mySet.count("c"); // returns 1 since 'c' appears once (remember that set keeps unique values)

mySet.count("x"); // returns 0 since it does not exist in the set

Checking if a value exists in a Set takes O(1) time.


Removing elements:

To delete a specified element from a set, you use the erase() method:

unordered_set<string> mySet({"b", "c", "c", "d"});

// Deleting elements
mySet.erase("c");

// Deleting a non-existent element
mySet.erase("z"); // does nothing

// mySet contains {"b", "d"}

The Set first checks if that key exists and if not, it does nothing and also doesn't raise an error, so it's safe to delete non-existing elements.

Removing an element from a Set takes O(1) time.


Iterating over the Set values:

If we want to iterate over values of the unordered_set, we can use the for loop along with the : operator:

unordered_set<string> mySet({"b", "c", "c", "d"});

for (string val : mySet) {
    cout << val << endl;
}

// This will print the following:
// d
// c
// b

Iterating over an unordered_set takes O(n) time.

Notice the order is not the same as we initiated. set keeps the data in random order


Space Complexity

A Set uses O(n) space, where n is the number of elements existing in the Set.


Assignment
Follow the Coding Tutorial and let's play with some Hash Sets.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 6
    Ctrl + Enter to check