Now is it the last one or the second one. Are these duplicates or multiplicities??
So, given that these are repeated several times and you need the second one I would use :
my %seen = ();
my $tmp
...
$seen{$_}++;
if ($_ ne $tmp && $seen{$tmp} == 1){
print $_;
$tmp =$_;
}elsif ($_ eq $tmp){
print $_ if $seen{$_} == 2;
}
however if it is the last one then:
my %seen = ();
my $tmp;
my $id;
...
if ($_ ne $tmp){
$seen{$tmp}=$id++;
}
and then in the second loop:
print $_ foreach (sort{$seen{$a}<=>$seen{$b}}keys %seen);
baxy