Linq is pretty powerful, but sometimes I can find myself rushing to make extensions and I later wonder if I could do it with native methods.
So, is it possible to implement the following without using an extension?
/// <summary>
/// Removes duplicates that are immediately clustered together.
/// </summary>
public static IEnumerable<TData> DistinctLocal<TData>(this IEnumerable<TData> enumerable)
{
bool yielded = false;
TData last = default(TData);
foreach (var item in enumerable)
{
if (yielded == false || last.Equals(item) == false)
{
last = item;
yielded = true;
yield return item;
}
}
}
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).
Here's how it can be done in plain LINQ:
int[] source = new[] { 3, 6, 1, 8, 4, 1, 1, 1, 7, 4, 2, 2 };
var result = source.Where((x, i) => i == 0 || !x.Equals(source.ElementAt(i - 1)));
However, this code should only be used when the underlying sequence implements an efficient indexer for handling the ElementAt
call, ideally in O(1) time. This is typically the case for IList<T>
implementations, such as arrays, but not for other collections, such as LinkedList<T>
.
Additionally, if you need to use this functionality regularly, there's nothing wrong with defining an extension method, which would be more maintainable than scattering this code (or any equivalent) all over the place. I would personally prefer to use your own extension method to avoid the risk of the performance issue on non-indexed collections.
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