# 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" ; }
In reply to Re: How to get 2nd highest value from an array?
by Nilotpal_1984
in thread How to get 2nd highest value from an array?
by rsiedl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |