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)
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.
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).
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.
.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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With