Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: How to get 2nd highest value from an array?

by choedebeck (Beadle)
on Dec 13, 2005 at 17:59 UTC ( [id://516467]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to How to get 2nd highest value from an array?

This was actually an interview question I had for my first job, only I had to do it in C. Here is a solution without a sort.
sub getSecondLargest { my (@list) = @_; my ($largest, $secondlargest); for(my $i = 0;$i<=$#list;$i++) { if($i == 0) { if($list[0] >= $list[1]) { ($largest, $secondlargest) = ($list[0], $list[1]); } else { ($largest, $secondlargest) = ($list[1], $list[0]); } } if($i >=3) { if($list[$i] >= $largest) { $secondlargest = $largest; $largest = $list[$i]; } elsif($list[$i] >= $secondlargest) { $secondlargest = $list[$i]; } } } return $secondlargest; }

Replies are listed 'Best First'.
Re^2: How to get 2nd highest value from an array?
by merlyn (Sage) on Dec 13, 2005 at 18:14 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://516467]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.