Open In App

C# | Create HashSet from another collection

Improve
Improve
Like Article
Like
Save
Share
Report

In C#, you can create a HashSet<T> from another collection by using the HashSet<T> constructor that takes an IEnumerable<T> parameter. Here’s an example:

C#




using System;
using System.Collections.Generic;
 
public class Program
{
    public static void Main()
    {
        // create a List of integers
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
 
        // create a HashSet from the List
        HashSet<int> numberSet = new HashSet<int>(numbers);
 
        // print out the contents of the HashSet using a foreach loop
        Console.WriteLine("Contents of HashSet:");
        foreach (int number in numberSet)
        {
            Console.WriteLine(number);
        }
    }
}


Output

Contents of HashSet:
1
2
3
4
5

A HashSet is an unordered collection of the unique elements. It comes under System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. You can create a HashSet from another collection by passing the collection as an argument while creating the object of HashSet. Below given are some examples to understand the implementation in a better way: Example 1: 

CSHARP




// C# code to Create HashSet
// from another collection
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Driver code
    public static void Main()
    {
 
        // Creating a HashSet of integers
        HashSet<int> mySet = new HashSet<int>();
 
        // Inserting even numbers less than
        // equal to 10 in HashSet mySet
        for (int i = 0; i < 5; i++) {
            mySet.Add(i * 2);
        }
 
        // Creating new HashSet mySet_new from already
        // Created HashSet mySet
        HashSet<int> mySet_new = new HashSet<int>(mySet);
 
        Console.WriteLine("The elements in newly created HashSet are : ");
 
        // Displaying the elements of newly created HashSet
        foreach(int i in mySet_new)
        {
            Console.WriteLine(i);
        }
    }
}


Example 2: 

CSHARP




// C# code to Create HashSet
// from another collection
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Driver code
    public static void Main()
    {
 
        // Creating a HashSet of strings
        HashSet<string> mySet = new HashSet<string>();
 
        // Inserting elements into HashSet mySet
        mySet.Add("Delhi");
        mySet.Add("Noida");
        mySet.Add("Chandigarh");
        mySet.Add("New York");
        mySet.Add("Bangalore");
 
        // Creating new HashSet mySet_new from already
        // Created HashSet mySet
        HashSet<string> mySet_new = new HashSet<string>(mySet);
 
        Console.WriteLine("The elements in newly created HashSet are : ");
 
        // Displaying the elements of newly created HashSet
        foreach(string i in mySet_new)
        {
            Console.WriteLine(i);
        }
    }
}




Last Updated : 22 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads