+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.

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,