score:5

Accepted answer

i think this should also work;

names.distinct().tolist().foreach( n => console.writeline(n));

score:0

var duplicates = suppliers_arr
    .groupby(i => i)
    .where(g => g.count() > 1)
    .select(g => g.key); 
if(duplicates.count() > 0){
    foreach (var d in duplicates)
    {
        modelstate.addformerror(string.format("{0} is duplicated",d.tostring()));
    }
}else{
     //no duplicates
}

score:1

you can use distinct.

try this:

string[] names = {"hello", "hello", "stack", "stack", "overflow", "overflow"};
var uniquenames = (from c in names select c).distinct();
foreach(string s in uniquenames) console.writeline(uniquenames);

score:2

despite the fact that some answer was marked accepted i think i can add some important information about the unexpected problems that you can come across in situations there you have objects in array and you need distinction to be based on some property of that object.

linq's distinct() function may fail to work properly in that situation, so you should use workaround like this(which give you the same result):

var elems = somearray.groupby(x => x.propertytocompare).select(y => y.first());

you can read more here: http://blog.jordanterrell.com/post/linq-distinct()-does-not-work-as-expected.aspx

score:8

here you go:

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

foreach(string distinctname in distinctnames) 
    console.writeline(distinctname);

Related Query