Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove duplicates from an Array using LINQ

Tags:

c#

linq

I'm a beginner in C# so I have a question, I have a lot of names in my database my question is how can I make script that shows the data of names with no doubles in LINQ? Here an example:

string[] names = {hello, hello, stack, stack, overflow, overflow};

I have no idea how to do this can someone create a simple script that shows it how to do. I can solve the rest myself.

like image 842
user2117650 Avatar asked Sep 19 '25 14:09

user2117650


1 Answers

Here you go:

string[] names = {"hello", "hello", "stack", "stack", "overflow", "overflow"};  
var distinctNames = names.Distinct();

foreach(String distinctName in distinctNames) 
    Console.WriteLine(distinctName);
like image 139
Colin Mackay Avatar answered Sep 21 '25 04:09

Colin Mackay