# How to get the highest value of an array of integers?

• July 1st, 2014, 01:03 PM
bihlas
How to get the highest value of an array of integers?
I am trying to write a method that returns the busiest hour in a logAnalyzer class that read web server data and analyze hourly access patterns and stores them in an array. My problem is, in order to get the busiest hour, I need to go through the hourCounts array to find the element with the biggest count.
• July 1st, 2014, 01:05 PM
KevinWorkman
Re: How to get the highest value of an array of integers?
Quote:

Originally Posted by bihlas
I need to go through the hourCounts array to find the element with the biggest count.

Have you tried doing exactly that?
• July 1st, 2014, 01:38 PM
bihlas
Re: How to get the highest value of an array of integers?
Going through all the elements of the array I am using a for loop, getting each count every time I loop through the array. how to specify in the loop body which from all the counts is the biggest in order to return the associated hour with that count.
• July 1st, 2014, 01:47 PM
KevinWorkman
Re: How to get the highest value of an array of integers?
I'm not really sure what you're asking. How would you do this by hand?

Say you have 100 index cards labeled 1-100 on the front and a random number on the back. Your job is to find the card with the highest random number, and then tell somebody else its index and the number. How would you do that?
• July 1st, 2014, 02:06 PM
Norm
Re: How to get the highest value of an array of integers?
One technique is to set the variable with the max value to the first element in the array, and then compare all the other elements in the array against it.
• July 2nd, 2014, 03:59 AM
bihlas
Re: How to get the highest value of an array of integers?
Code java:

```/** * Returns the busiest hour of the day. */ public int busiestHour() { int theBusiestHour = 0; // The hourCounts array has length = 24. for(int hour = 0; hour < hourCounts.length; hour++) {   } return theBusiestHour; }```

To make it more clear about what I want.

Norm I cant set the variable with the max value to the first element of the array because it would always return as the busiest hour to be midnight. I need help to understand how to find the the biggest count in the array in order to assign the associated hour to theBusiestHour variable.
• July 2nd, 2014, 05:20 AM
GregBrannon
Re: How to get the highest value of an array of integers?
I don't think you understood Norm. Use a variable as the highest value set to the first value in the array:

int highestValue = hourCounts( 0 );
int highestElement = 0;

Compare each of the remaining array elements to highestValue using a loop that examines each element of the array, and if a new higher value is found, update the highestValue AND highestElement variables. Return the highestElement value, because that points to the highestValue in the array hourCounts().

Good luck!