C# Program to get distinct element from a sequence


Set a sequence and add elements.

List<int> ID = new List<int> { 120, 111, 250, 111, 120, 300, 399, 450 };

Use Distinct() method to get distinct element from the above list.

IEnumerable<int> res = ID.AsQueryable().Distinct();

Let us see the complete code.

Example

 Live Demo

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.AsQueryable().Distinct();
      foreach (int arr in res) {
         Console.WriteLine(arr);
      }
   }
}

Output

120
111
250
300
399
450

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements