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