Find the shortest element in an array
3 answers
There is no method .Exclude
(or extension method) for the array, nor did he say he wants to change the collection type for the new array. Your usage is .Aggregate
outstanding, so let's take it one step further and use .Aggregate
it to make an exception!
Like this:
string shortestName = names.Aggregate((n1, n2) => n1.Length < n2.Length ? n1 : n2);
string nonArrayString = names.Aggregate((n1, n2) => n2 != shortestName ? n1 + " " + n2 : n1);
string[] newNames = nonArrayString.Split(' ');
David Hedlunds' technique is still much better because it is easier to read! There are no bonus points for writing the hardest answer ... lol
+1
a source to share