Eric
Eric

Reputation: 3792

Need a performant way to return all duplicates from a list

I am going to be working with a collection that has about 500,000 items in it and am looking for a decent way of getting all the duplicates. After looking through this post I see that the most popular solution is to use a hashed set. But what if I want to get all Cars that have the color red not just Car4 and Car5?

Car1.Color = Red;

Car2.Color = Blue;

Car3.Color = Green;

Car4.Color = Red;

Car5.Color = Red;

Given the problem what would be a reasonably fast way to do this?

EDIT: I saw in that post that the code below could easily be changed to fit my needs. And I'm not sure there is really a better way to solve the problem but I will leave the post up just to see.

var duplicates = from car in cars
                 group car by car.Color into grouped
                 from car in grouped
                 select car;

Upvotes: 2

Views: 128

Answers (3)

Chris
Chris

Reputation: 2895

You could look into creating various lookup tables using a Dictionary. For instance, if you wanted to do lookup on Car.Color, you would have a Dictionary>, so any time a new car is added you also add the Color dictionary.

This has a tradeoff of additional memory usage & Add time in order to benefit from faster lookups, and makes the most sense only if you're going to be looking up collections of Cars by the same key field.

Otherwise, using Enumerable.ToLookup() is the way to go. Please see this link for an explanation of how ToLookup() works :

http://msdn.microsoft.com/en-us/library/bb549073.aspx

Upvotes: 0

Steve B
Steve B

Reputation: 37660

You can use group by value

class Car {
    public Color { get; set; }
}

void Main()
{
     List<Car> cars = GetList(); // not important
     var grouped = cars.GroupBy(c=>c.Color);
     var duplicates = cars.Where(g=>g.Count()>1);

}

Upvotes: 1

dtb
dtb

Reputation: 217313

You can use the Enumerable.ToLookup Extension Method to group the cars by color and retrieve all cars of one color:

var cars = new List<Car> { car1, car2, car3, car4, car5 };
var lookup = cars.ToLookup(car => car.Color);
var redCars = lookup[Red];
// redCars == { car1, car4, car5 }

Upvotes: 9

Related Questions