Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance between check exists before add to list and distinct in linq

In the foreach loop, I want to add the Products to a List, but I want this List to not contain duplicate Products, currently I have two ideas solved.

1/ In the loop, before adding the Product to the List, I will check whether the Product already exists in the List, otherwise I will add it to the List.

foreach (var product in products)
{
    // code logic
    if(!listProduct.Any(x => x.Id == product.Id))
    {
        listProduct.Add(product);
    }
}

2/. In the loop, I will add all the Products to the List even if there are duplicate products. Then outside of the loop, I would use Distinct to remove duplicate records.

foreach (var product in products)
{
    // code logic
        listProduct.Add(product);
}
listProduct  = listProduct.Distinct().ToList();

I wonder in these two ways is the most effective way. Or have any other ideas to be able to add records to the List to avoid duplication ??

like image 733
Tran Audi Avatar asked Oct 30 '25 00:10

Tran Audi


1 Answers

I'd go for a third approach: the HashSet. It has a constructor overload that accepts an IEnumerable. This constructor removes duplicates:

If the input collection contains duplicates, the set will contain one of each unique element. No exception will be thrown.

Source: HashSet<T> Constructor

usage:

List<Product> myProducts = ...;
var setOfProducts = new HashSet<Product>(myProducts);

After removing duplicates there is no proper meaning of setOfProducts[4].

Therefore a HashSet is not a IList<Product>, but an ICollection<Product>, you can Count / Add / Remove, etc, everything you can do with a List. The only thing you can't do is fetch by index

like image 158
Harald Coppoolse Avatar answered Nov 01 '25 16:11

Harald Coppoolse