唯一字符串的有效列表 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/918742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Efficient list of unique strings C#
提问by
What is the most efficient way to store a list of strings ignoring any duplicates? I was thinking a dictionary may be best inserting strings by writing dict[str] = false; and enumerating through the keys as a list. Is that a good solution?
存储字符串列表而忽略任何重复项的最有效方法是什么?我在想字典可能最好通过编写 dict[str] = false; 来插入字符串;并通过键作为列表进行枚举。这是一个很好的解决方案吗?
回答by JP Alioto
If you are using .NET 3.5, the HashSetshould work for you.
如果您使用 .NET 3.5,HashSet应该适合您。
The HashSet<(Of <(T>)>) class provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.
HashSet<(Of <(T>)>) 类提供高性能的集合操作。集合是不包含重复元素且其元素没有特定顺序的集合。
回答by AndrewB
This is not part of the the system namespace but have used the Iesi.Collections from http://www.codeproject.com/KB/recipes/sets.aspxwith NHibernate. It has support for hashed set along with sorted set, dictionary set, and so on. Since it has been used with NHibernate it has been used extensively and very stable. This also does not require .Net 3.5
这不是系统命名空间的一部分,而是在NHibernate 中使用了来自http://www.codeproject.com/KB/recipes/sets.aspx的 Iesi.Collections 。它支持散列集以及排序集、字典集等。自从它与 NHibernate 一起使用以来,它已被广泛使用且非常稳定。这也不需要 .Net 3.5
回答by Perpetualcoder
You can look to do something like this
你可以做这样的事情
var hash = new HashSet<string>();
var collectionWithDup = new []{"one","one","two","one","two","zero"};
// No need to check for duplicates as the Add method
// will only add it if it doesn't exist already
foreach (var str in collectionWithDup)
hash.Add(str);
回答by scone
I'm not sure if this counts as a good answer, but when faced with the need for a unique set that maintains insertion order, I compromised with a HashSet and a List side-by-side. In this case, whenever you add to the set, do the following:
我不确定这是否算得上是一个好的答案,但是当面临需要一个保持插入顺序的唯一集合时,我同时使用 HashSet 和 List 进行了妥协。在这种情况下,每当您添加到集合时,请执行以下操作:
if(hashSet.Add(item))
orderList.Add(item);
When removing items, make sure to remove them from both. Thus, as long as you can be sure that nothing else added items to the list, you'll have an insertion-ordered unique set!
删除项目时,请确保将它们从两者中删除。因此,只要您可以确定没有其他任何东西向列表中添加了项目,您就会拥有一个按插入顺序排列的唯一集合!
回答by Priyang
Use HashSet, no need to check .Contains() , just add your items in list and if its duplicate it will not add it.
使用 HashSet,无需检查 .Contains() ,只需将您的项目添加到列表中,如果重复,则不会添加它。
HashSet<int> uniqueList = new HashSet<int>();
uniqueList.Add(1); // List has values 1
uniqueList.Add(2); // List has values 1,2
uniqueList.Add(1); // List has values 1,2
Console.WriteLine(uniqueList.Count); // it will return 2
回答by Alexey Solonets
Here is another solution without using the HashSet
.
这是不使用HashSet
.
var items = new List<string>() { "one", "one", "two", "one", "two", "zero" };
var uniqueItems = items.Where((item, index) => items.IndexOf(item) == index);
It was adopted from this thread: javascript - Unique values in an array
它是从这个线程中采用的:javascript - 数组中的唯一值
Test:
测试:
using FluentAssertions;
uniqueItems.Count().Should().Be(3);
uniqueItems.Should().BeEquivalentTo("one", "two", "zero");
Performance test for List
, HashSet
and SortedSet
. 1 million iterations:
对于性能测试List
,HashSet
和SortedSet
。100 万次迭代:
List: 564 ms
HashSet: 487 ms
SortedSet: 1932 ms
回答by Dave Hollingsworth
You could also use Linq as in:
您还可以使用 Linq,如下所示:
using System.Linq;
var items = new List<string>() { "one", "one", "two", "one", "two", "zero" };
List<string> distinctItems = items.Distinct().ToList();