tags:

views:

179

answers:

2

I have following array

string[] words = { "cherry", "apple", "blueberry", "banana", "mango", "orange", "pineapple" };

I want to find the Max and Min no. of alphabets. e.g. Max = 9 (for pineapple) and Min = 5 (for apple)

Which is the shortest method to do this.

+11  A: 

You can use the Min and Max methods:

var min = words.Min(w=> w.Length);  // 5
var max = words.Max(w=> w.Length);  // 9
CMS
Beautiful. Didn't even know you could express functional things like that in C#.
Brandon Pelfrey
A: 

The most efficient is to simply loop through the strings:

int min = Int32.MaxValue;
int max = 0;
foreach (s in words) {
   min = Math.Min(min, s.Length);
   max = Math.Max(max, s.Length);
}
Guffa