C# 3.0: Need to return duplicates from a List<>

前端 未结 8 1871
无人及你
无人及你 2020-12-23 19:05

I have a List<> of objects in C# and I need a way to return those objects that are considered duplicates within the list. I do not need the Distinct resultset, I need a

相关标签:
8条回答
  • 2020-12-23 19:58

    Create a new Dictionary<Color, Car> foundColors and a List<Car> carsToDelete

    Then you iterate through your original list of cars like so:

    foreach(Car c in listOfCars)
    {
        if (foundColors.containsKey(c.Color))
        {
            carsToDelete.Add(c);
        }
        else
        {
            foundColors.Add(c.Color, c);
        }
    }
    

    Then you can delete every car that's in foundColors.

    You could get a minor performance boost by putting your "delete record" logic in the if statement instead of creating a new list, but the way you worded the question suggested that you needed to collect them in a List.

    0 讨论(0)
  • 2020-12-23 20:01

    I inadvertently coded this yesterday, when I was trying to write a "distinct by a projection". I included a ! when I shouldn't have, but this time it's just right:

    public static IEnumerable<TSource> DuplicatesBy<TSource, TKey>
        (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    {
        HashSet<TKey> seenKeys = new HashSet<TKey>();
        foreach (TSource element in source)
        {
            // Yield it if the key hasn't actually been added - i.e. it
            // was already in the set
            if (!seenKeys.Add(keySelector(element)))
            {
                yield return element;
            }
        }
    }
    

    You'd then call it with:

    var duplicates = cars.DuplicatesBy(car => car.Color);
    
    0 讨论(0)
提交回复
热议问题