tags:

views:

948

answers:

4

Hey all,

What is the fasters / most efficient way of getting all the unique items out of a C# list?

I have List that possiably has multiple repeating items in it and only want the unique values within the list.

Thanks

+6  A: 

You can use the Distinct method to return an IEnumerable<T> of distinct items:

var uniqueItems = yourList.Distinct();

And if you need the sequence of unique items returned as a List<T>, you can add a call to ToList:

var uniqueItemsList = yourList.Distinct().ToList();
LukeH
The OP was looking for a fast/efficient method. This is not it. Calling `yourList.Distinct().ToList()` requires two full iterations over the enumerable, and additionally is based off `IEqualityComparer`, which is slower than `GetHashCode`.
Noldorin
Is this faster/more efficient than a HashSet<T>? I don't think so. Not bothering to downvote, though :-)
Vinay Sajip
@Noldorin, @Vinay: *If* the OP needs the distinct items returned as a `List` then they'll need to call `ToList`, regardless of whether they use `Distinct` or construct a `HashSet`. Having said that, you're right that a `HashSet` will probably have better performance than `Distinct` in most circumstances.
LukeH
+2  A: 

You can use Distinct extension method from LINQ

aku
+11  A: 

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
Vinay Sajip
Must agree; others solve the problem, yours solves the cause :)
Noon Silk
A `HashSet` won't maintain any ordering, which may or may not be an issue for the OP.
LukeH
Why the downvote? The question asks about fastest/most efficient, and doesn't require ordering to be maintained.
Vinay Sajip
thanks guys, I don't require the items to be ordered. This works great.
domgreen
A: 

Apart from the Distinct extension method of LINQ, you could use a HashSet<T> object that you initialise with your collection. This is most likely more efficient than the LINQ way, since it uses hash codes (GetHashCode) rather than an IEqualityComparer).

In fact, if it's appropiate for your situation, I would just use a HashSet for storing the items in the first place.

Noldorin
A `HashSet` won't maintain any ordering, which may or may not be an issue for the OP.
LukeH
@Luke: Even so, ordering would have no meaning after calling `Distinct`...
Noldorin
@Luke: The question asks about fastest/most efficient, and doesn't require ordering to be maintained.
Vinay Sajip
@Noldorin: Why not? `Distinct` should/does iterate the list in order (although I'm not sure if that's actually guaranteed in any spec).
LukeH
@Luke: Oh, I was thinking of indexing really. And anyway, efficiency was mentioned in the OP, while order wasn't (though that's open question) - `HashSet` is the way to go if you want good performance.
Noldorin