By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,067 Members | 864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,067 IT Pros & Developers. It's quick & easy.

highest value in a an array

P: n/a
hey all,
i have an int[] array and was just wondering if there is a way to get the
highest value in the array?

for instance,
int[] myValues = new int[] { 0, 1, 2 }

highest value is 2.

thanks,
rodchar
Apr 3 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi rodchar,

Basically you will need to sort the array and then choose the last element
(assuming you have sorted it in lowest to highest order).

e.g.
int[] integerArray = new int[10]{23,8,34,1,19,263,11,4,2,84};
Array.Sort(integerArray);

The element integerArray[9] will now contain 263 which is the highest value
in the array. There are many ways to sort an array but I think the Static
Sort of the Array class is pretty optimised as it goes.

Hope this helps
wibberlet
Development blog at http://wibberlet.blogspot.com

"rodchar" wrote:
hey all,
i have an int[] array and was just wondering if there is a way to get the
highest value in the array?

for instance,
int[] myValues = new int[] { 0, 1, 2 }

highest value is 2.

thanks,
rodchar
Apr 3 '07 #2

P: n/a
Wibberlet <Wi*******@discussions.microsoft.comwrote:
Basically you will need to sort the array and then choose the last element
(assuming you have sorted it in lowest to highest order).
Well, that's one way to do it. Its complexity is O(n log n) however,
whereas just finding the maximal value only needs to be O(n).

You just go through the array, remembering the current highest value:

int max = int.MinValue;

foreach (int value in integerArray)
{
if (value max)
{
max = value;
}
}

LINQ will make all of this rather easier.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 3 '07 #3

P: n/a
Thanks everyone.

"rodchar" wrote:
hey all,
i have an int[] array and was just wondering if there is a way to get the
highest value in the array?

for instance,
int[] myValues = new int[] { 0, 1, 2 }

highest value is 2.

thanks,
rodchar
Apr 4 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.