-2

How do I remove dupliactes in a collection? For example:

List<double> somelist=new List<double>{2, 2, 3};

What LINQ method should I use?

And try to make it simple please.

Sinan AKYAZICI
  • 3,942
  • 5
  • 35
  • 60
  • 1
    Just use a [`HashSet`](http://msdn.microsoft.com/en-us/library/bb359438.aspx) and you won't have any duplicates to begin with. – Rob Hruska Oct 20 '13 at 23:33
  • Are you trying to get a new collection without the duplicates, or are you do you want the same collection just without the dups? – Gabe Oct 20 '13 at 23:33

2 Answers2

6

Enumerable.Distinct is probably what you're looking for.

var somelist = new List<double>{2, 2, 3}.Distinct();

// somelist now contains 2, 3
Matthew
  • 24,703
  • 9
  • 76
  • 110
  • And it simply gets rid of duplicates? –  Oct 20 '13 at 23:33
  • 3
    @EdwardKarak You should refer to the documentation for [`Enumerable.Distinct`](http://msdn.microsoft.com/en-us/library/system.linq.enumerable.distinct.aspx) – User 12345678 Oct 20 '13 at 23:34
  • As long as your `T` from `IEnumerable` implements `GetHashCode` and `Equals` methods property `Enumerable.Distinct` will work fine. – MarcinJuraszek Oct 20 '13 at 23:37
2

I would suggest using a HashSet<double> instead of List<double>.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445