Method HasDuplicates
| Edit this page View SourceHasDuplicates<T>(IEnumerable<T>)
Checks if sequence contains duplicates.
Declaration
public static bool HasDuplicates<T>(this IEnumerable<T> source)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence to check. |
Returns
Type | Description |
---|---|
bool | true if any element of the sequence |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the source sequence. |
Remarks
This method executes immediately.
Examples
The following code example demonstrates how to check if a sequence has duplicate values using HasDuplicates
.
var seq1 = new[] { "foo", "bar", "baz" };
var seq2 = new[] { "foo", "bar", "baz", "foo", };
var seq3 = new[] { "foo", "bar", "baz", "FOO", };
// determine if a sequence has duplicate items
var result = seq1
.HasDuplicates();
Console.WriteLine($"Has Duplicates: {result}");
result = seq2
.HasDuplicates();
Console.WriteLine($"Has Duplicates: {result}");
result = seq3
.HasDuplicates();
Console.WriteLine($"Has Duplicates: {result}");
// This code produces the following output:
// Has Duplicates: False
// Has Duplicates: True
// Has Duplicates: False
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
HasDuplicates<T>(IEnumerable<T>, IEqualityComparer<T>?)
Checks if sequence contains duplicates, using the specified element equality comparer.
Declaration
public static bool HasDuplicates<T>(this IEnumerable<T> source, IEqualityComparer<T>? comparer)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence to check. |
IEqualityComparer<T> | comparer | The equality comparer to use to determine whether or not keys are equal. If null, the
default equality comparer for |
Returns
Type | Description |
---|---|
bool | true if any element of the sequence |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the source sequence. |
Remarks
This method executes immediately.
Examples
The following code example demonstrates how to check if a sequence has duplicate values using HasDuplicates
.
var seq1 = new[] { "foo", "bar", "baz" };
var seq2 = new[] { "foo", "bar", "baz", "foo", };
var seq3 = new[] { "foo", "bar", "baz", "FOO", };
// determine if a sequence has duplicate items
var result = seq1
.HasDuplicates(
StringComparer.OrdinalIgnoreCase);
Console.WriteLine($"Has Duplicates: {result}");
result = seq2
.HasDuplicates(
StringComparer.OrdinalIgnoreCase);
Console.WriteLine($"Has Duplicates: {result}");
result = seq3
.HasDuplicates(
StringComparer.OrdinalIgnoreCase);
Console.WriteLine($"Has Duplicates: {result}");
// This code produces the following output:
// Has Duplicates: False
// Has Duplicates: True
// Has Duplicates: True
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
HasDuplicates<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)
Checks if sequence contains duplicates according to a specified key selector function.
Declaration
public static bool HasDuplicates<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The sequence to check. |
Func<TSource, TKey> | keySelector | A function to extract the key for each element. |
Returns
Type | Description |
---|---|
bool | true if any element of the sequence |
Type Parameters
Name | Description |
---|---|
TSource | Type of the source sequence. |
TKey | The type of key to distinguish elements by. |
Remarks
This method executes immediately.
Examples
The following code example demonstrates how to check if a sequence has duplicate values using HasDuplicates
.
var seq1 = new[] { "f", "ba", "qax" };
var seq2 = new[] { "f", "ba", "qax", "fuba", };
var seq3 = new[] { "f", "ba", "qax", "FUBA", };
// determine if a sequence has duplicate items
var result = seq1
.HasDuplicates(
x => x[0..1]);
Console.WriteLine($"Has Duplicates: {result}");
result = seq2
.HasDuplicates(
x => x[0..1]);
Console.WriteLine($"Has Duplicates: {result}");
result = seq3
.HasDuplicates(
x => x[0..1]);
Console.WriteLine($"Has Duplicates: {result}");
// This code produces the following output:
// Has Duplicates: False
// Has Duplicates: True
// Has Duplicates: False
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
HasDuplicates<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IEqualityComparer<TKey>?)
Checks if sequence contains duplicates according to a specified key selector function, using the specified element equality comparer.
Declaration
public static bool HasDuplicates<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The sequence to check. |
Func<TSource, TKey> | keySelector | A function to extract the key for each element. |
IEqualityComparer<TKey> | comparer | The equality comparer to use to determine whether or not keys are equal. If null, the
default equality comparer for |
Returns
Type | Description |
---|---|
bool | true if any element of the sequence |
Type Parameters
Name | Description |
---|---|
TSource | Type of the source sequence. |
TKey | The type of key to distinguish elements by. |
Remarks
This method executes immediately.
Examples
The following code example demonstrates how to check if a sequence has duplicate values using HasDuplicates
.
var seq1 = new[] { "f", "ba", "qax" };
var seq2 = new[] { "f", "ba", "qax", "fuba", };
var seq3 = new[] { "f", "ba", "qax", "FUBA", };
// determine if a sequence has duplicate items
var result = seq1
.HasDuplicates(
x => x[0..1],
StringComparer.OrdinalIgnoreCase);
Console.WriteLine($"Has Duplicates: {result}");
result = seq2
.HasDuplicates(
x => x[0..1],
StringComparer.OrdinalIgnoreCase);
Console.WriteLine($"Has Duplicates: {result}");
result = seq3
.HasDuplicates(
x => x[0..1],
StringComparer.OrdinalIgnoreCase);
Console.WriteLine($"Has Duplicates: {result}");
// This code produces the following output:
// Has Duplicates: False
// Has Duplicates: True
// Has Duplicates: True
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|