Effective C # Unique String List

What is the most efficient way to keep a list of rows ignoring any duplicates? I thought a dictionary could best insert lines by writing dict [str] = false; and listing through the keys as a list. This is a good decision?

+73
c # unique-values
May 28 '09 at 1:13
source share
6 answers

If you are using .NET 3.5, HashSet should work for you.

The HashSet <(Of <(T>)>) class provides high-performance operations. A set is a collection that does not contain duplicate elements and whose elements do not have a special order.

+96
May 28 '09 at 1:17 a.m.
source share

You can watch 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); 
+16
May 28 '09 at 3:04 a.m.
source share

I'm not sure if this is considered a good answer, but, faced with the need to create a unique set that supports the insertion order, I compromised using the HashSet and List side by side. In this case, when you add to the set, follow these steps:

 if(hashSet.Add(item)) orderList.Add(item); 

When deleting items, be sure to remove them from both. Thus, as long as you can be sure that nothing is added to the list, you will have a unique set, ordered in order!

+12
Jun 13 2018-12-12T00:
source share

Use a HashSet, you do not need to check .Contains (), just add your items to the list, and if its duplicate does not add it.

  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 
+5
Feb 21 '14 at 8:56
source share

This is not part of the system namespace, but they used Iesi.Collections from http://www.codeproject.com/KB/recipes/sets.aspx with NHibernate. It supports a hashed set along with a sorted set, a set of dictionaries, etc. Since it was used with NHibernate, it was used widely and very stably. It also does not require .Net 3.5

+2
May 28 '09 at 1:42 a.m.
source share

Here is another solution without using a 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

Test:

 using FluentAssertions; uniqueItems.Count().Should().Be(3); uniqueItems.Should().BeEquivalentTo("one", "two", "zero"); 

Performance testing for List , HashSet and SortedSet . 1 million iterations:

 List: 564 ms HashSet: 487 ms SortedSet: 1932 ms 

Test source code (gist)

+1
Aug 04 '16 at 10:55 on
source share



All Articles