It depends on what you mean. If you need one representative list and another for the remaining duplicates, you can 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 need one list for those elements that are unique, and another for those that are not:
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 a bit inefficient, as it lists twice. Something like this would be slightly better:
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); }
source share