Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: A good and efficient implementation of IEnumerable<T>.HasDuplicates

Does anyone have a good and efficient extension method for finding if a sequence of items has any duplicates?

Guess I could put return subjects.Distinct().Count() == subjects.Count() into an extension method, but kind of feels that there should be a better way. That method would have to count elements twice and sort out all the distict elements. A better implementation should return true on the first duplicate it finds. Any good suggestions?

I imagine the outline could be something like this:

public static bool HasDuplicates<T>(this IEnumerable<T> subjects)
{
    return subjects.HasDuplicates(EqualityComparer<T>.Default);
}

public static bool HasDuplicates<T>(this IEnumerable<T> subjects, IEqualityComparer<T> comparer)
{
    ...
}

But not quite sure how a smart implementation of it would be...

like image 240
Svish Avatar asked Jul 15 '09 21:07

Svish


Video Answer


3 Answers

public static bool HasDuplicates<T>(this IEnumerable<T> subjects)
{
    return HasDuplicates(subjects, EqualityComparer<T>.Default);
}

public static bool HasDuplicates<T>(this IEnumerable<T> subjects, IEqualityComparer<T> comparer)
{
    HashSet<T> set = new HashSet<T>(comparer);
    foreach (T item in subjects)
    {
        if (!set.Add(item))
            return true;
    }

    return false;
}
like image 74
Sam Harwell Avatar answered Nov 15 '22 10:11

Sam Harwell


This is in production code. Works great:

public static bool HasDuplicates<T>(this IEnumerable<T> sequence, IEqualityComparer<T> comparer = null) {
    var set = new HashSet<T>(comparer);
    return !sequence.All(item => set.Add(item));
}
like image 36
Rich Armstrong Avatar answered Nov 15 '22 10:11

Rich Armstrong


I think the simplest extension method is the following.

public static bool HasDuplicates<T>(this IEnumerable<T> enumerable) {
  var hs = new HashSet<T>();
  foreach ( var cur in enumerable ) {
    if ( !hs.Add(cur) ) {
      return false;
    }
  }
}
like image 43
JaredPar Avatar answered Nov 15 '22 09:11

JaredPar