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;
using System.Linq;
namespace Carl.RemoveDuplicatesList
{
class Program
{
static void Main(string[] args)
{
// Create list with non-unique values
List<string> l = new List<string>();
l.Add("1");
l.Add("2");
l.Add("2"); // duplicate
l.Add("3");
l.Add("4");
l.Add("4"); // duplicate
l.Add("5");
// Make new unique list
List<string> uniqueList = l.Distinct().ToList();
// Write to console
uniqueList.ForEach(i => Console.WriteLine($"{i}"));
Console.ReadLine();
}
}
}
[/sourcecode]
This outputs:
![]()
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

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.