This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes second maximum value from the array Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "second maximum value from the array" Watch "second maximum value from the array" New topic
Author

second maximum value from the array

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1303
How to find second maximum value from the array ? I know how to find the maximum value from the array.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10043
    
    6

here is a list of numbers

2, 19, 496, 22, 84, 196, 12

Tell me how YOU would find the second largest? work it out with pencil and paper.


Never ascribe to malice that which can be adequately explained by stupidity.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1303
2, 19, 496, 22, 84, 196, 12

Take each element and compare with other elements and swap the place for sorting/descending.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
No, there is a much better way to do it. You don't need any swapping or sorting. Just make sure to choose the correct initial value before you get to the array.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1303
Just make sure to choose the correct initial value before you get to the array. I do not understand this statement.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
Before you start searching, you need a value for max or whatever your largest result is to be. Choosing the wrong value for that will, of course, mean you fail to find the correct maximum. The same applies if you are looking for second largest.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10043
    
    6

let's try this approach...

How would you find the maximum? Once you know how to do that, there are several other approaches you can take, but we should start with baby steps.

 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: second maximum value from the array
 
Similar Threads
Problem with the date 2005-07-14
To get Maximum and minimum value from an array
displaying the most highly repeated entry in an ArrayList
How to find the maximum value in an array
How to find small value from Integer array?