I have a method as below. Method return either false/true either when list contains duplicates or not. I would like to extend my method to say for instance (optional) that i
public static bool ContainsDuplicates(this IEnumerable items, IEnumerable itemsToExclude = null)
{
if (itemsToExclude == null) itemsToExclude = Enumerable.Empty();
return items.Except(itemsToExclude)
.GroupBy(n => n)
.Any(c => c.Count() > 1);
}
But i'd prefer this implementation because it's more performant:
public static bool ContainsDuplicates(this IEnumerable items, IEnumerable itemsToExclude = null)
{
if (itemsToExclude == null) itemsToExclude = Enumerable.Empty();
HashSet set = new HashSet();
return !items.Except(itemsToExclude).All(set.Add);
}