-1

I would like to remove the duplicate elements from a List. Some elements of the list looks like this:

Book  23
Book  22
Book  19
Notebook 23
Notebook 22
Notebook 19
Pen 23
Pen 22
Pen 19

I would like to keep in the list just

Book 23
Notebook 23
Pen 23

How can i do that ?

Emil Dumbazu
  • 662
  • 4
  • 22
  • 37

3 Answers3

2

What about basic looping?

List<string> nodup = dup.Distinct().ToList();
List<int> remIndex = new List<int>();
for (int nIdx = 0; nIdx < nodup.Count; nIdx++)
{
    string[] strArr = nodup[nIdx].Split(' ');
    if (String.Compare(strArr[1], "23", true) != 0)
        remIndex.Add(nIdx);
}
foreach (int remIdx in remIndex)
    nodup.RemoveAt(remIdx);

Hope this is of some help...

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
1

try this

  List<Person> distinctPeople = allPeople
  .GroupBy(p => p.PersonId)
  .Select(g => g.First())
  .ToList();

from this discussion

use your column names

Community
  • 1
  • 1
robasta
  • 4,621
  • 5
  • 35
  • 53
0

Try this

list.Sort();

Int32 index = 0; while (index < list.Count - 1) {
if (list[index] == list[index +1])
list.RemoveAt(index); else index++; }

JayOnDotNet
  • 398
  • 1
  • 5
  • 17