Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • HashSet<> in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 387
    Comment on it

    In c#, HashSet is a disjoint group of an unique elements. We can a apply a multiple operation on HashSet such as Add, Remove, Contains etc. HashSet is also  allow to apply a standard set of operations such as union, intersection, and symmetric difference.


    Syntax for creating a HashSet:

    HashSet<int> HSet1 = new HashSet<int>();
    HashSet<int> HSet2 = new HashSet<int>();

     

    Code for adding an elements in the HashSet<>:
    We use an add() method to add an element to the HashSet.

    for (int i = 1; i < =10; i++)
    {
       HSet1.Add(2 * i);
    }
    for (int j = 1; j < =10; j++)
    {
       HSet2.Add(100 * j);
    }
    

     

    Code for Printing the elements of HashSet using foreach loop:

    namespace HashSetSampleApplication
    {
        class ProgramForHashSet
        {
            static void Main(string[] args)
            {
                HashSet<int> HSet1 = new HashSet<int>();
                HashSet<int> HSet2 = new HashSet<int>();
                for (int i = 1; i <=10; i++)
                {
                    HSet1.Add(2 * i);
                }
                for (int j = 1; j <=10; j++)
                {
                    HSet2.Add(100 * j);
                }
                Console.WriteLine("The table of two(2) is:");
                foreach (int hset1 in HSet1)
                {
                    Console.WriteLine(hset1);
                }
                Console.WriteLine("The table of Hundered(100) is:");
                foreach (int hset2 in HSet2)
                {
                    Console.WriteLine(hset2);
                }
            }
        }
    }
    

     

    Output:

     

    Code for Removing the elements from the HashSet<>:

    namespace HashSetSampleApplication
    {
        class ProgramForHashSet
        {
            static void Main(string[] args)
            {
                HashSet<int> HSet = new HashSet<int>();
                for (int i = 1; i <=10; i++)
                {
                    HSet.Add(2 * i);
                }
            Console.WriteLine("The table of Two(2) is:");
                foreach (int hset in HSet)
                {
                    Console.WriteLine(hset);
                }
              for (int i = 1; i <= 10; i++)
                {
                    if (i < 8)
                    {
                        HSet.Remove(2 * i);
                    }
                }
                Console.WriteLine("After Removing the elements from the hashset :  ");
                foreach (int hset in HSet)
                {
                    Console.WriteLine(hset);
                }
            }
        }
    }
    

     

    Output:

     

    Code for finding an element from the HashSet<>:

    namespace HashSetSampleApplication
    {
        class ProgramForHashSet
        {
            static void Main(string[] args)
            {
                HashSet<int> HSet1 = new HashSet<int>();
                for (int i = 1; i <=10; i++)
                {
                    HSet1.Add(2 * i);
                }
                Console.WriteLine("The table of two(2) is:");
                foreach (int hset1 in HSet1)
                {
                    Console.WriteLine(hset1);
                }
                Console.WriteLine("ITem 6 is found in HashSet : " + HSet1.Contains(6));
                Console.WriteLine("ITem 10 is found in HashSet : " + HSet1.Contains(10));
                Console.WriteLine("ITem 18 is found in HashSet : " + HSet1.Contains(18));
            }
        }
    }
    

     

    Output:

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: