473,800 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8813
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,2 63,11,4,2,84};
Array.Sort(inte gerArray);

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*******@disc ussions.microso ft.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.co m>
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
2305
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
1623
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 cycle through all 255 if that's possible. any ideas? Thanks!
21
13487
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 getting second highest value in an integer array. One method I know of is to make the 1st pass through the array and find the highest number. In the second pass we can find the highest number which is less than the number we obtained in the 1st pass.
6
24341
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 the Sort Method ( ) but doesn't seem like it can do the job or am i missing something?
7
3358
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
16101
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. Note that N = 10. */ #define N 10 #include <iostream> using namespace std;
17
7722
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
10661
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
9691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10276
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10035
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.