my @a=qw(1 2 3 4 6 3 1); my %h = map {$_=>1} @a; my ($max) = reverse sort keys %h; for (1..$max) { print "gap at $_ \n" unless exists $h{$_} }