开发者

Filter duplicates from list

开发者 https://www.devze.com 2023-02-09 14:41 出处:网络
I have List<Location> locations. The Location class has a property Coordinates - assume a string.

I have List<Location> locations.

The Location class has a property Coordinates - assume a string.

How can I remove Locations that have duplicate Coordinates an开发者_Python百科d place them into a separate list? Having two lists - one for duplicates and one without.


Creating an IEqualityComparer< Locations > is going to be one of your first tasks (which allows you to compare the objects based on properties you've chosen).

If you want to get unique items using Linq then you can use the Distinct() method.

You could then remove the items from your original list which will leave you with a collection of duplicates.

var distinctObjects = originalList.Distinct();
var duplicateList = originalList.Except(distinctObjects);

You will need to use a custom equality comparer for distinct, but not for except.


It depends on what you mean, really. If you want one representative list, and another one for the remaining duplicates, you could do:

var locationsByCoordinates = locations.ToLookup(location => location.Coordinates);

var distinct = locationsByCoordinates.Select(group => group.First())
                                     .ToList();

var duplicates = locationsByCoordinates.SelectMany(group => group.Skip(1))
                                       .ToList(); 

On the other hand, if you want one list for those items that are unique, and another for those that aren't:

var distinct = locationsByCoordinates.Where(group => group.Count() == 1)
                                     .Select(group => group.Single())
                                     .ToList();

var duplicates = locationsByCoordinates.Where(group => group.Count() != 1)
                                       .SelectMany(group => group)
                                       .ToList();

This is slightly inefficient though since it enumerates the lookup twice. Slightly better would be something like:

var distinct = new List<Location>();
var duplicates = new List<Location>();

foreach(var group in locationsByCoordinates)
{
    var target = group.Count() == 1 ? distinct : duplicates;
    target.AddRange(group);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消