Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster alternatives to .Distinct()

Tags:

c#

.net

linq

I'm making a video game where performance is critical.

I'm using the .Distinct() extension method to get unique value from a List. Is there a faster way to do so? (even if it means having many more lines of code)

like image 253
Vittorio Romeo Avatar asked May 11 '11 21:05

Vittorio Romeo


People also ask

How to replace count distinct?

You can replace SQL COUNT DISTINCT with the keyword Approx_Count_distinct to use this function from SQL Server 2019. You can explore more on this function in The new SQL Server 2019 function Approx_Count_Distinct.

What does distinct () do in C#?

C# Linq Distinct() method removes the duplicate elements from a sequence (list) and returns the distinct elements from a single data source. It comes under the Set operators' category in LINQ query operators, and the method works the same way as the DISTINCT directive in Structured Query Language (SQL).

Does distinct preserve order?

Java Stream distinct() MethodIf the stream is ordered, the encounter order is preserved. It means that the element occurring first will be present in the distinct elements stream.


2 Answers

.Distinct is an O(n) call.
You can't get any faster than that.

However, you should make sure that your GetHashCode (and, to a lesser extent, Equals) is as fast as possible.

Depending on your scenario, you may be able to replace the List<T> with a HashSet<T>, which will prevent duplicates from being inserted in the first place. (yet has O(1) insertion)

However, Always profile your code before jumping to conclusions about what needs to be faster.

like image 113
SLaks Avatar answered Sep 24 '22 21:09

SLaks


Does it have to be a List?

Would it be possible to switch from List, to HashSet? HashSet prevents objects from being inserted into the list more than once in the first place, so the Distinct is already done.

like image 39
David Yaw Avatar answered Sep 22 '22 21:09

David Yaw