获取独特的项目从列表 [英] Getting unique items from a list
本文介绍了获取独特的项目从列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是从列表中获得所有不同的项目最快/最有效的方法是什么?
What is the fastest / most efficient way of getting all the distinct items from a list?
我有一个列表与LT;字符串方式>
这可能有它的多个重复的项目,只需要在列表中的唯一值
I have a List<string>
that possibly has multiple repeating items in it and only want the unique values within the list.
推荐答案
使用一个 HashSet的&LT; T&GT;
。例如:
Use a HashSet<T>
. For example:
var items = "A B A D A C".Split(' ');
var unique_items = new HashSet<string>(items);
foreach (string s in unique_items)
Console.WriteLine(s);
打印
A
B
D
C
这篇关于获取独特的项目从列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文