-4

i used this code:

List<string> lists=new List<string>("apple","orange","banana","apple","mang0","orange");
string names;
names=lists.Distinct()

is that correct?

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
user1445857
  • 19
  • 1
  • 6

2 Answers2

0

No, the variable names has to be a collection. The Distinct method returns an enumerator, so you probably want to enumerate the result and realise it as a list:

List<string> names = lists.Distinct().ToList();
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

You can sort the list, then check two and two:

list.Sort();
Int32 index = 0;
while (index < list.Count - 1)
{
  if (list[index] == list[index + 1])
    list.RemoveAt(index);
else
    index++;
}
Botonomous
  • 1,746
  • 1
  • 16
  • 39