从列表中获取唯一项目 [英] Getting unique items from a list

查看:33
本文介绍了从列表中获取唯一项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从列表中获取所有不同项目的最快/最有效的方法是什么?

我有一个 List,其中可能有多个重复项,并且只想要列表中的唯一值.

解决方案

使用 HashSet.例如:

var items = "A B A D A C".Split(' ');var unique_items = new HashSet(items);foreach(unique_items 中的字符串 s)Console.WriteLine(s);

印刷品

<前>一种乙DC

What is the fastest / most efficient way of getting all the distinct items from a list?

I have a List<string> that possibly has multiple repeating items in it and only want the unique values within the list.

解决方案

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);

prints

A
B
D
C

这篇关于从列表中获取唯一项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆