Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq - Locally Distinct

Tags:

c#

linq

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;
        }
    }
}
like image 271
Meirion Hughes Avatar asked Oct 04 '13 09:10

Meirion Hughes


People also ask

How to use Distinct in LINQ Query?

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).


1 Answers

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.

like image 169
Douglas Avatar answered Oct 14 '22 07:10

Douglas