Method Duplicates
| Edit this page View SourceDuplicates<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>?)
Returns all duplicate elements of the given source.
Declaration
public static IEnumerable<TSource> Duplicates<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource>? comparer = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
IEqualityComparer<TSource> | comparer | The equality comparer to use to determine whether one |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | All elements that are duplicated. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements in the source sequence. |
Remarks
This operator uses deferred execution and streams its results.
Examples
The following code example demonstrates how to get the duplicated elements of a sequence using Duplicates
.
var sequence = new[] { "foo", "bar", "baz", "foo", };
// determine if a sequence has duplicate items
var result = sequence.Duplicates();
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [foo]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|