C# Program to get distinct element from a sequence

The Distinct() method in C# is used to remove duplicate elements from a sequence and return only unique values. This method is part of LINQ (Language Integrated Query) and works with any collection that implements IEnumerable<T>.

Syntax

Following is the syntax for the Distinct() method −

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

Parameters

  • comparer (optional) − An IEqualityComparer<T> to compare values 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 List

Here's how to remove duplicate integers from a list −

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

class Demo {
   static void Main() {
      List<int> ID = new List<int> { 120, 111, 250, 111, 120, 300, 399, 450 };
      
      // get distinct elements
      IEnumerable<int> res = ID.Distinct();
      
      Console.WriteLine("Original list:");
      foreach (int id in ID) {
         Console.Write(id + " ");
      }
      
      Console.WriteLine("<br>\nDistinct elements:");
      foreach (int arr in res) {
         Console.WriteLine(arr);
      }
   }
}

The output of the above code is −

Original list:
120 111 250 111 120 300 399 450 

Distinct elements:
120
111
250
300
399
450

Using Distinct() with String Array

The Distinct() method also works with strings and other data types −

using System;
using System.Linq;

class Program {
   static void Main() {
      string[] names = { "John", "Alice", "Bob", "Alice", "John", "Charlie" };
      
      var distinctNames = names.Distinct();
      
      Console.WriteLine("Distinct names:");
      foreach (string name in distinctNames) {
         Console.WriteLine(name);
      }
   }
}

The output of the above code is −

Distinct names:
John
Alice
Bob
Charlie

Using Distinct() with Custom Objects

When working with custom objects, Distinct() compares object references by default. To compare by property values, you need to override Equals() and GetHashCode() methods −

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

class Person {
   public string Name { get; set; }
   public int Age { get; set; }
   
   public override bool Equals(object obj) {
      if (obj is Person other) {
         return Name == other.Name && Age == other.Age;
      }
      return false;
   }
   
   public override int GetHashCode() {
      return (Name?.GetHashCode() ?? 0) ^ Age.GetHashCode();
   }
   
   public override string ToString() {
      return $"{Name} ({Age})";
   }
}

class Program {
   static void Main() {
      List<Person> people = new List<Person> {
         new Person { Name = "John", Age = 25 },
         new Person { Name = "Alice", Age = 30 },
         new Person { Name = "John", Age = 25 },
         new Person { Name = "Bob", Age = 35 }
      };
      
      var distinctPeople = people.Distinct();
      
      Console.WriteLine("Distinct people:");
      foreach (Person person in distinctPeople) {
         Console.WriteLine(person);
      }
   }
}

The output of the above code is −

Distinct people:
John (25)
Alice (30)
Bob (35)

How It Works

The Distinct() method uses the default equality comparer to determine uniqueness. It maintains the order of first occurrence and removes subsequent duplicates. The method is deferred, meaning it doesn't execute until the result is enumerated.

Conclusion

The Distinct() method is a powerful LINQ extension that efficiently removes duplicate elements from any enumerable collection. It preserves the order of first occurrence and works with primitive types, strings, and custom objects when proper equality comparison is implemented.

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

347 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements