Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท LINQ

LINQ Distinct() and DistinctBy() methods

Reference for LINQ Distinct and DistinctBy in C#: removing duplicates, equality rules for custom types, comparers, ordering and performance.

Distinct() returns the elements of a sequence with duplicates removed. DistinctBy() (.NET 6+) does the same but decides what counts as a duplicate from a key you select, and returns the first element for each key.

Equality is decided by Equals() and GetHashCode() of the element or key (or by a comparer you supply). That is why records and tuples work out of the box, while a plain class does not unless it overrides both methods.

C#
var unique = numbers.Distinct();
var onePerCity = people.DistinctBy(p => p.City);

Try it

C# Distinct and DistinctBy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Equality decides what is a duplicate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static IEnumerable<T> Distinct<T>(this IEnumerable<T> source);
public static IEnumerable<T> Distinct<T>(this IEnumerable<T> source, IEqualityComparer<T>? comparer);
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector);
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> source,
    Func<T, TKey> keySelector, IEqualityComparer<TKey>? comparer);
Name Description
keySelector Computes the key whose uniqueness is checked.
comparer Optional equality rules; null uses the default for the type.

Returns: a lazy sequence of the first occurrence of each distinct element (or key).

Errors: ArgumentNullException for a null source or key selector.

Usage notes

  • Order is preserved. The first occurrence of each value is returned in the order it appears in the source. This is how current .NET behaves, but the documentation does not promise it, so call OrderBy() if you rely on a specific order.
  • Complexity: O(n) time with a hash set holding the distinct items, so it needs O(k) memory for k distinct values.
  • Custom classes: override Equals(object?) and GetHashCode() together, make the type a record, or pass a comparer. Overriding only one breaks hash-based operations.
  • Streaming: Distinct() returns items as soon as it sees a new one, so it works on infinite sequences if you add Take().
  • null is a valid element and counts as one distinct value.
  • Case-insensitive text: use StringComparer.OrdinalIgnoreCase; the first spelling wins.
  • Duplicates only? To find the values that occur more than once, use GroupBy(x => x).Where(g => g.Count() > 1).
  • In place: to deduplicate while collecting, add to a HashSet<T> and check the return value of Add().