在 C# 中初始化哈希集
初始化哈希集。
var h = new HashSet<string>(arr1);
上面我们在哈希集中设置了一个数组。以下为该数组 -
string[] arr1 = { "electronics", "accessories”, "electronics", };
以下是一个示例,展示了如何在 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)); } }
Advertisement