C# 中的 HashSet 是什么?
C# 中的 HashSet 集合可以提供一个数组中去重字符串或元素。它是一个经过优化的集合。
来看一个使用 C# HashSet 去重字符串的例子 −
示例
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
string[] arr1 = {
"one",
"two",
"two",
"one",
"three"
};
Console.WriteLine(string.Join(",", arr1));
// HashSet
var h = new HashSet(arr1);
// eliminates duplicate words
string[] arr2 = h.ToArray();
Console.WriteLine(string.Join(",", arr2));
}
}声明 HashSet 集合。
var h = new HashSet<string7gt;(arr1);
在数组上使用它来删除重复单词 −
string[] arr2 = h.ToArray();
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP