Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Array, How to make data in an array distinct from each other?

C# Array, How to make data in an array distinct from each other? For example

string[] a = {"a","b","a","c","b","b","c","a"}; 

how to get

string[]b = {"a","b","c"}
like image 490
Prince OfThief Avatar asked Nov 12 '10 08:11

Prince OfThief


2 Answers

Easiest way is the LINQ Distinct() command :

var b = a.Distinct().ToArray();
like image 86
Aidan Avatar answered Oct 06 '22 00:10

Aidan


You might want to consider using a Set instead of an array. Sets can't contain duplicates so adding the second "a" would have no effect. That way your collection of characters will always contain no duplicates and you won't have to do any post processing on it.

like image 36
brain Avatar answered Oct 05 '22 23:10

brain