Would
int[] nums = { 2, 3, 3, 4, 2, 1, 6, 7, 10 };
var distinct = nums.Distinct();
always return 2, 3, 4, 1, 6, 7, 10
in that 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.
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).
The defined behavior of Enumerable.Distinct is that it will return an unordered collection (Documentation).
However the current implementation of Distinct in Linq to Objects will preserve order. This is not guaranteed for other LINQ providers though and the behavior should not be relied upon.
I think the word "unordered" means the same order of the original sequence.
Hence, the caller should decide whether to sort the result or not.
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