To remove duplicates from a C# list using Linq, do the following.
- Define a new list. Ours has 7 elements, with “2” and “4” repeating
- Use Distinct().ToList() to make a new list with the distinct elements
- Print out to the console
using System.Collections.Generic; |
namespace Carl.RemoveDuplicatesList |
static void Main( string [] args) |
List< string > l = new List< string >(); |
List< string > uniqueList = l.Distinct().ToList(); |
uniqueList.ForEach(i => Console.WriteLine($ "{i}" )); |
This outputs:

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.
IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.
THANK YOU, AND LET'S KEEP LEARNING TOGETHER.
CARL
https://www.youtube.com/carldesouza
Thank you for the very clear example of how to obtain a unique list. But what if you have a generic collection list? I have that and I am trying to obtain a unique list based on one field in that collection.
Write a function which takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once.
Hallo
Please i Need help
I have a Array with 7 element , but i would like to calculate the Addition of element that he comes one time
Example : int [] Array { 10, 15, 15, 10,5,15,3}
Addition ist : 5 + 3 = 8
10 comes two time : Array[0] and Array[3]
the same with 15
only 5 and 3 appears one time
please how i can write the code?
Use distinct().toarray()
it will remove all the duplicate values from your array.