Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining the min and max of a two-dimensional array using LINQ

How would you obtain the min and max of a two-dimensional array using LINQ? And to be clear, I mean the min/max of the all items in the array (not the min/max of a particular dimension).

Or am I just going to have to loop through the old fashioned way?

like image 945
devuxer Avatar asked Jun 15 '10 21:06

devuxer


3 Answers

Since Array implements IEnumerable you can just do this:

var arr = new int[2, 2] {{1,2}, {3, 4}};
int max = arr.Cast<int>().Max();    //or Min
like image 98
Lee Avatar answered Oct 17 '22 07:10

Lee


This seems to work:

IEnumerable<int> allValues = myArray.Cast<int>();
int min = allValues.Min();
int max = allValues.Max();
like image 30
Mark Byers Avatar answered Oct 17 '22 06:10

Mark Byers


here is variant:

var maxarr = (from int v in aarray select v).Max();

where aarray is int[,]

like image 36
dondublon Avatar answered Oct 17 '22 07:10

dondublon