Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by Nilotpal_1984 (Initiate)
on Jun 24, 2017 at 05:09 UTC ( [id://1193428]=note: print w/replies, xml ) Need Help??


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

# code to get second highest number without sort. #!/usr/bin/perl use strict ; my @arr = qw(4 4 0 -1 -2 2 0 1) ; my $f = $arr[0] ; my $s = $arr[0] ; for (my $i=1 ; $i<@arr; $i++) { if ($arr[$i] > $f) { $s = $f ; $f = $arr[$i] ; } elsif (($arr[$i] > $s || (($f == $s))) && $arr[$i]!=$f) { $s = $arr[$i] ; } } if ($f == $s) { print "\n there is no second greatest number" ; } else { print "\n second highest number : $s" ; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1193428]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2025-12-06 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (85 votes). Check out past polls.

    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.