+In this article we will discuss how to create a HashSet of integers and add elements in it.

Creating a HashSet Object for integer

This hashset object can contain only unique integers.

Adding Element in a HashSet

To add an element in a HashSet we will use its following member function,

This add() function will try to add element in HashSet and if element already exists in HashSet then it will return false;

Checkout the following example,

Output:

 

In the previous example, when we tried  to add “12” again into the HashSet then add(0 function returned false. This is because HashSet only contains unique elements.