Alan B
Alan B

Reputation: 2289

Removing Duplicates from bottom of Generic List

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?

Upvotes: 5

Views: 425

Answers (3)

ChaseMedallion
ChaseMedallion

Reputation: 21764

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.

Upvotes: 9

Mateus Schneiders
Mateus Schneiders

Reputation: 4903

You could also implement a custom add method to maintain the latest records:

public class IdentifierList : List<Identifier>
{
    public void Add(Identifier item)
    {
        this.RemoveAll(x => x.Name == item.Name);
        base.Add(item);
    }
}

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" };

IdentifierList list = new IdentifierList ();
list.Add(idn1);
list.Add(idn2);
list.Add(idn3);
list.Add(idn4);
list.Add(idn5);
list.Add(idn6);

Upvotes: 1

Sorax
Sorax

Reputation: 2203

You could Group and check if any Count is > 1

var distinctWorked = !(res
.GroupBy(a => a.Name)
.Select(g => new{g.Key, Count = g.Count()})
.Any(a => a.Count > 1));

Upvotes: 0

Related Questions