HashSet<> in C#

Introduction

HashSet is an unordered collection that contains the unique elements. We can apply various operations on a HashSet like, Add, Remove, Contains etc. HashSet also provides standard set operations such as union, intersection, and symmetric difference.

Creation of Hashset<>

The following line of code describes how to create the hashset in C#:

HashSet<int> firstset = new HashSet<int>();

HashSet<int> secondset = new HashSet<int>();

 

Add elements in the Hashset<>

 

To add an element to the Hashset we use the add() method like:

 

for (int i = 1; i < 10; i++)
{

   firstset.Add(5 * i);

}

for (int j = 1; j < 10; j++)

{

   secondset.Add(10 * j);

}

 

Prints the elements of hashset using foreach loop

 

Write the following code to print the elements of the hashset:

 

namespace ConsoleApplication14

{

    class Program

    {

        static void Main(string[] args)

        {

            HashSet<int> firstset = new HashSet<int>();

            HashSet<int> secondset = new HashSet<int>();

            for (int i = 1; i <= 10; i++)

            {

                firstset.Add(5 * i);

            }

            for (int j = 1; j <=10; j++)

            {

                secondset.Add(10 * j);

            }

            Console.WriteLine("The table of five(5) is:");

            foreach (int a in firstset)

            {

                Console.WriteLine(a);

            }

            Console.WriteLine("The table of Ten(10) is:");

            foreach (int b in secondset)

            {

                Console.WriteLine(b);

            }

        }

    }

}

 

Output is

hashset1.jpg

 

Remove the elements from the Hashset<>

 

To remove an element we use the Remove() method like:

 

namespace ConsoleApplication14

{

    class Program

    {

        static void Main(string[] args)

        {

            HashSet<int> firstset = new HashSet<int>();

            for (int i = 1; i <= 10; i++)

            {

                firstset.Add(5 * i);

            }

            Console.WriteLine("The table of five(5) is:");

            foreach (int a in firstset)

            {

                Console.WriteLine(a);

            }

            for (int i = 1; i <= 10; i++)

            {

                if (i < 8)

                {

                    firstset.Remove(5 * i);

                }

            }

            Console.WriteLine("After Removal the elements the hashset is as,");

            foreach (int a in firstset)

            {

                Console.WriteLine(a);

            }

        }

    }

}

 

 Output is

 hashset2.jpg

Finding an element:

For finding an element in the HashSet we use the Contains methods. This method finds an element and if the element is present in the list then it gives TRUE otherwise FALSE. For example:

namespace ConsoleApplication14

{

    class Program

    {

        static void Main(string[] args)

        {

            HashSet<int> firstset = new HashSet<int>();

            for (int i = 1; i <= 10; i++)

            {

                firstset.Add(5 * i);

            }

            Console.WriteLine("The table of five(5) is:");

            foreach (int a in firstset)

            {

                Console.WriteLine(a);

            }

            Console.WriteLine("ITem contain is:" + firstset.Contains(15));

            Console.WriteLine("ITem contain is:" + firstset.Contains(55));

            Console.WriteLine("ITem contain is:"+firstset.Contains(45));

        }

    }

}

 

Output is

 hashset3.jpg

Summary

In this article I explain the concept of HashSet and some operations like add, remove and contain in the hashset.

Up Next
    Ebook Download
    View all

    Pristine

    Read by 0 people
    Download Now!
    Learn
    View all