C# Linq Distinct() Method

The Distinct() method in C# LINQ is used to remove duplicate elements from a collection and return only unique elements. This method is available for any collection that implements IEnumerable<T> and preserves the order of first occurrence.

Syntax

Following is the basic syntax for the Distinct() method −

IEnumerable<T> Distinct()
IEnumerable<T> Distinct(IEqualityComparer<T> comparer)

Parameters

  • comparer (optional) − An IEqualityComparer<T> to compare elements for equality. If not provided, the default equality comparer is used.

Return Value

Returns an IEnumerable<T> containing distinct elements from the source sequence.

Using Distinct() with Integer Collections

The following example demonstrates removing duplicate integers from a list −

using System;
using System.Linq;
using System.Collections.Generic;

class Demo {
   static void Main() {
      List<int> points = new List<int> { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 };
      
      // Get distinct elements from the list
      IEnumerable<int> distinctPoints = points.Distinct();
      
      Console.WriteLine("Original list:");
      foreach (int point in points) {
         Console.Write(point + " ");
      }
      
      Console.WriteLine("<br>\nDistinct elements:");
      foreach (int point in distinctPoints) {
         Console.Write(point + " ");
      }
   }
}

The output of the above code is −

Original list:
5 10 5 20 30 30 40 50 60 70 

Distinct elements:
5 10 20 30 40 50 60 70 

Using Distinct() with String Collections

The Distinct() method also works with string collections, performing case-sensitive comparison by default −

using System;
using System.Linq;
using System.Collections.Generic;

class Program {
   static void Main() {
      List<string> names = new List<string> { 
         "Alice", "Bob", "alice", "Charlie", "Bob", "David" 
      };
      
      var distinctNames = names.Distinct();
      
      Console.WriteLine("Distinct names (case-sensitive):");
      foreach (string name in distinctNames) {
         Console.WriteLine(name);
      }
   }
}

The output of the above code is −

Distinct names (case-sensitive):
Alice
Bob
alice
Charlie
David

Using Distinct() with Custom Objects

For custom objects, Distinct() uses the object's Equals() and GetHashCode() methods to determine uniqueness −

using System;
using System.Linq;
using System.Collections.Generic;

class Student {
   public int Id { get; set; }
   public string Name { get; set; }
   
   public override bool Equals(object obj) {
      if (obj is Student other) {
         return this.Id == other.Id;
      }
      return false;
   }
   
   public override int GetHashCode() {
      return Id.GetHashCode();
   }
   
   public override string ToString() {
      return $"ID: {Id}, Name: {Name}";
   }
}

class Program {
   static void Main() {
      List<Student> students = new List<Student> {
         new Student { Id = 1, Name = "John" },
         new Student { Id = 2, Name = "Jane" },
         new Student { Id = 1, Name = "Johnny" }, // Duplicate ID
         new Student { Id = 3, Name = "Bob" }
      };
      
      var distinctStudents = students.Distinct();
      
      Console.WriteLine("Distinct students by ID:");
      foreach (var student in distinctStudents) {
         Console.WriteLine(student);
      }
   }
}

The output of the above code is −

Distinct students by ID:
ID: 1, Name: John
ID: 2, Name: Jane
ID: 3, Name: Bob

Conclusion

The Distinct() method is a powerful LINQ operator that efficiently removes duplicate elements from collections while preserving the order of first occurrence. It works with built-in types using default equality comparers and can be customized for complex objects by overriding Equals() and GetHashCode() methods.

Updated on: 2026-03-17T07:04:35+05:30

908 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements