tags:

views:

1877

answers:

3

I am trying to filter an IEnumerable object of the duplicate values, so I would like to get the distinct values from it, for example, lets say that it holds days:

monday tuesday wednesday wednesday

I would like to filter it and return:

monday tuesday wednesday

What is the most efficient way to do this in .net 2.0?

+3  A: 
Dictionary<object, object> list = new Dictionary<object, object>();
foreach (object o in enumerable)
    if (!list.ContainsKey(o))
    {
        // Do the actual work.
        list[o] = null;
    }

Dictionary will use a hash table to hold keys therefore lookup is efficient.

Sorting will be O(n log(n)) at best. A hash table with an efficient hash function often outperforms it (O(1) lookups).

Mehrdad Afshari
"foreach(object o in enumerable) list[o] = true;" also works
Jimmy
Yeah, it works if you don't have any other things to do with the data, which is unlikely. If you mean iterating through the o.Keys collection once again, it's not a good idea as it doesn't preserve the order of items in the original collection and it's definitely slower.
Mehrdad Afshari
+1  A: 

Make another IEnumerable. Sort the original. For each item in the original, if the new one doesn't contain the old one, add it.

Jason Lepack
A: 

Another alternative is to use HashSet<T> - a HashSet doesn't allow duplicate items to be used and doesn't require a key/value pair.

Rob
question was about 2.0
Jimmy
Wow, that's my mistake - I didn't realize that HashSet was part of System.Core. My mistake!
Rob