
Initialize a HashSet.
var h = new HashSet<string>(arr1);
Above, we set an array in HashSet. The following is an array -
string[] arr1 = {
"electronics",
"accessories”,
"electronics",
};The following example shows how to implement a HashSet in C# -
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
string[] arr1 = {
"electronics",
"accessories”,
"electronics",
};
Console.WriteLine(string.Join(",", arr1));
// HashSet
var h = new HashSet(arr1);
// eliminates duplicate words
string[] arr2 = h.ToArray();
Console.WriteLine(string.Join(",", arr2));
}
}The above is the detailed content of Initializing a HashSet in C#. For more information, please follow other related articles on the PHP Chinese website!