Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to print unique values from a list
In C#, you can extract unique values from a List using the Distinct() LINQ method. This method filters out duplicate elements and returns only the unique values, maintaining the order of their first occurrence.
Syntax
Following is the syntax for using Distinct() method −
List<T> uniqueList = originalList.Distinct().ToList();
The Distinct() method returns an IEnumerable<T>, so we use ToList() to convert it back to a List<T>.
Using Distinct() with Integer List
Here's how to remove duplicate integers from a list and display only unique values −
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
List<int> list = new List<int>();
list.Add(55);
list.Add(45);
list.Add(55);
list.Add(65);
list.Add(73);
list.Add(24);
list.Add(65);
Console.WriteLine("Initial List...");
foreach(int a in list) {
Console.WriteLine("{0}", a);
}
List<int> myList = list.Distinct().ToList();
Console.WriteLine("Unique elements...");
foreach(int a in myList) {
Console.WriteLine("{0}", a);
}
}
}
The output of the above code is −
Initial List... 55 45 55 65 73 24 65 Unique elements... 55 45 65 73 24
Using Distinct() with String List
The Distinct() method also works with string lists and other data types −
using System;
using System.Collections.Generic;
using System.Linq;
public class StringDemo {
public static void Main() {
List<string> fruits = new List<string> {"Apple", "Banana", "Apple", "Orange", "Banana", "Grape"};
Console.WriteLine("Original fruit list:");
foreach(string fruit in fruits) {
Console.WriteLine(fruit);
}
List<string> uniqueFruits = fruits.Distinct().ToList();
Console.WriteLine("\nUnique fruits:");
foreach(string fruit in uniqueFruits) {
Console.WriteLine(fruit);
}
}
}
The output of the above code is −
Original fruit list: Apple Banana Apple Orange Banana Grape Unique fruits: Apple Banana Orange Grape
Using HashSet for Unique Values
An alternative approach is using HashSet<T>, which automatically stores only unique elements −
using System;
using System.Collections.Generic;
public class HashSetDemo {
public static void Main() {
List<int> numbers = new List<int> {10, 20, 10, 30, 20, 40};
Console.WriteLine("Original list:");
foreach(int num in numbers) {
Console.Write(num + " ");
}
HashSet<int> uniqueNumbers = new HashSet<int>(numbers);
Console.WriteLine("<br>\nUnique numbers using HashSet:");
foreach(int num in uniqueNumbers) {
Console.Write(num + " ");
}
}
}
The output of the above code is −
Original list: 10 20 10 30 20 40 Unique numbers using HashSet: 10 20 30 40
Comparison
| Method | Maintains Order | Performance | Usage |
|---|---|---|---|
| Distinct().ToList() | Yes | Good for small to medium lists | When order matters |
| HashSet<T> | No | Better for large datasets | When order doesn't matter |
Conclusion
The Distinct() LINQ method is the most straightforward way to extract unique values from a C# list while preserving order. For scenarios where order doesn't matter and performance is critical, consider using HashSet<T> instead.
