Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Duplicates from bottom of Generic List

Tags:

c#

linq

generics

I am trying to remove duplicates item from bottom of generic list. I have class defined as below

public class Identifier
{
    public string Name { get; set; }
}

And I have defined another class which implements IEqualityComparer to remove the duplicates from List

public class DistinctIdentifierComparer : IEqualityComparer<Identifier>
{
    public bool Equals(Identifier x, Identifier y)
    {
        return x.Name == y.Name;
    }

    public int GetHashCode(Identifier obj)
    {
        return obj.Name.GetHashCode();
    }
}

However, I am trying to remove the old items and keep the latest. For example if I have list of identifier defined as below

Identifier idn1 = new Identifier { Name = "X" };
Identifier idn2 = new Identifier { Name = "Y" };
Identifier idn3 = new Identifier { Name = "Z" };
Identifier idn4 = new Identifier { Name = "X" };
Identifier idn5 = new Identifier { Name = "P" };
Identifier idn6 = new Identifier { Name = "X" };

List<Identifier> list =  new List<Identifier>();
list.Add(idn1);
list.Add(idn2);
list.Add(idn3);
list.Add(idn4);
list.Add(idn5);
list.Add(idn6);

And I have implemented

var res = list.Distinct(new DistinctIdentifierComparer());

How do I make sure by using distinct that I am keeping idn6 and removing idn1 and idn4?

like image 253
Alan B Avatar asked Oct 01 '12 23:10

Alan B


1 Answers

Most LINQ operators are order-preserving: the API of Distinct() says it will take the first instance of each item it comes across. If you want the last instance, just do:

var res = list.Reverse().Distinct(new DistinctIdentifierComparer());

Another option that would avoid you having to define an explicit comparer would be:

var res = list.GroupBy(i => i.Name).Select(g => g.Last());

From MSDN:

The IGrouping objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping. Elements in a grouping are yielded in the order they appear in source.

like image 58
ChaseMedallion Avatar answered Oct 21 '22 10:10

ChaseMedallion