0

Possible Duplicate:
Remove duplicates from array

I have a list of items. I want to select all the items without repetition. How to do that in C#?

Community
  • 1
  • 1
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392

3 Answers3

7

You're looking for the aptly-named Distinct() method.
You may need to write an IEqualityComparer<T>.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    Hmmm... knowing what a pedant I am... the OP says "**all** items without repetition", `Distinct` selects a subset of items where each result is unique within the result set ;) You're probably right in what they actually want though. – Lazarus Feb 24 '11 at 12:48
  • 2
    @Lazarus: Is there any other way to understand the question? I can't think of one. – SLaks Feb 24 '11 at 12:55
  • I know it's unlikely but what about a set of data that has common values (i.e. 1,2,2,2,4,1,2,3,1,4,4) but when presented must not have identical values adjacent to one another (i.e. 1,2,4,2,1,2,3,2,4,1,4). Crazy but I've seen much barmier requests here ;) I still think you have the right answer however. – Lazarus Feb 24 '11 at 13:01
  • @SLaks coming from Google and yes, there is another way and not, this answer doen't apply. I don't want the records with duplicates at all: like HAVING COUNT(*) < 2 – Leandro Bardelli Jan 10 '21 at 18:12
1

This should do it:

list.Distinct();
jevakallio
  • 35,324
  • 3
  • 105
  • 112
1

I suppose you mean you want to remove duplicates. Use Distinct

int[] ints = new int[] { 1, 2, 3, 4, 5, 4, 3, 2 };
var uniqueInts = ints.Distinct();
Console.WriteLine(string.Join(", ", uniqueInts));
Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108