How to do DistinctBy but keep the last element of each duplicate in C#, in source order
Enumerable.DistinctBy keeps the first element per key. To keep the last one and still return results in the order they appear in the source, buffer the input, scan it backwards with a HashSet, and yield the survivors forwards. Eight implementations checked for order, null keys, double enumeration and argument validation on .NET 10.0.10 and .NET 11 RC3, and benchmarked on .NET 10.