# Thread: Finding Second largest number

1. Member
Join Date
Dec 2011
Posts
8
Rep Power
0

## Finding Second largest number

The purpose of this code is to do 3 things.
1st - Find the max time (given in the array times)
2nd - Find the 2nd max time (given in the array times)
3rd - Find the person that correlates with the max and 2nd max time (the names are given in array names)

I managed to figure out how to find the max time.
Im having trouble finding the 2nd time (attempt shown)
Can anyone point me towards the right direction to do the 3rd part?

Java Code:
```class Marathon {
public static void main(String[] arguments){

String names[] ={
"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex",
"Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda",
"Aaron", "Kate"};

int times[] ={
341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299,
343, 317, 265};

int max=0;
int max2 = 0;
int counter, counter2;

//finding max time
for (counter = 0; counter < times.length; counter++) {
while(times[counter]>max){
max=times[counter];
}
}

System.out.println("maximum number is = " + max);

//find 2nd max time
for(int x: times){
while(x > max2 && max2 != max){
max2 = x;
}
}
System.out.println("2nd maximum number is = " + max2);
}
}```

2. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
6

## Re: Finding Second largest number

You don't need a nested loop to find the max number in an array.
Just run on the array and keep asking if max is smaller then the value of the array in the current index then max gets the array value.
Try to think what you do with the second one.

3. Member
Join Date
Dec 2011
Posts
3
Rep Power
0

## Re: Finding Second largest number

Solution :
class Marathon {
public static void main(String[] arguments){

String names[] ={
"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex",
"Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda",
"Aaron", "Kate"};

int times[] ={
341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299,
343, 317, 265};

int max=0;
int max2 = 0;
int counter, counter2;

//finding max time
for (counter = 0; counter < times.length; counter++) {
if(times[counter]>max){
max2=max;
max=times[counter];
}
else if(times[counter]>max2)
{
max2=times[counter];
}
}

System.out.println("maximum number is = " + max);
System.out.println("2nd maximum number is = " + max2);
}
}

4. ## Re: Finding Second largest number

The initialization of max and max2 is incorrect; e.g. what happens if all the numbers are negative?

kind regards,

Jos

6. ## Re: Finding Second largest number

What if you created a separate method to run through an array and find the index position of the max number? Then once you've got that index, you could create a copy of the array with the highest number removed and find the highest number in the second array.

Java Code:
```int idxMaxNum;
for (int count=array.length-1; count>0; count--) {
idxMaxNum = (array[count] > array[count-1])?
count : count-1;
}
return idxMaxNum;```
Last edited by ozzyman; 12-29-2011 at 04:54 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•