473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

highest value in a an array

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
3 8789
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: shank | last post by:
How do you return the highest value in a recordset of maybe 100 records? Is it necessary to run 2 recordsets? I was hoping it was as simple as Max(), but no luck. thanks
12
by: Jozef | last post by:
Hello, Is there an easy way to determine the highest point in an array that contains a value? I'm dimensioning an array to hold up to 255 items, but if it only contains three, I don't want to...
21
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of...
6
by: Ada | last post by:
hello folks, is there a way to retrieve the highest value in the ArrayList? let say i have a value in the array: {1, 4, 15, 3, 7} it should return a value 15 as the result. i've looked at...
7
by: Jan | last post by:
Hi there, Is there a fast way to get the highest value from an array? I've got the array strStorage(intCounter) I tried something but it all and's to nothing If someone good helpme, TIA
3
by: lostncland | last post by:
I am working on this program. The array has 10 scores. (there is more to this program.) Does the last "for" section make sense? /*Defines a global constant called N throughout the file. ...
17
by: rhitz1218 | last post by:
Hi, I'm trying to create a function that will sort a number's digits from highest to lowest. For example 1000 - will become 0001 or 1234 to 4321
3
by: MyMarlboro | last post by:
i wonder am i make a mistake or not... use Data::Dumper; my @array= qw(1 2 3 4 5 6 8 9 2 5); my $highest=@array; foreach my $number (@array) { if (@array > $highest) { $highest = @array;
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.