在C#中,Vector<T> 类型通常用于表示多维向量,而不是存储一系列元素的集合
以下是使用HashSet删除重复元素的示例:
using System;
using System.Collections.Generic;
using System.Numerics;
class Program
{
static void Main()
{
// 创建一个包含重复元素的Vector<int>
Vector<int> vectorWithDuplicates = new Vector<int>(new int[] { 1, 2, 3, 4, 4, 5, 6, 7, 7, 8 });
// 将Vector<int>转换为List<int>
List<int> listWithDuplicates = new List<int>(vectorWithDuplicates);
// 使用HashSet删除重复元素
HashSet<int> hashSet = new HashSet<int>(listWithDuplicates);
// 将HashSet转换回List<int>
List<int> uniqueList = new List<int>(hashSet);
// 输出去重后的结果
Console.WriteLine("Unique elements:");
foreach (int element in uniqueList)
{
Console.WriteLine(element);
}
}
}
这段代码首先创建了一个包含重复元素的 Vector<int>。然后,它将 Vector<int> 转换为 List<int>,接着使用 HashSet<int> 删除重复元素。最后,将 HashSet<int> 转换回 List<int> 并输出去重后的结果。
请注意,此示例仅适用于整数类型的向量。对于其他类型,只需相应地更改 Vector<T>、List<T> 和 HashSet<T> 中的泛型参数即可。