use warnings; use strict; my @arr = (29,24,0,24,24,12,0,10,29,10,19,17,15,13,1,12,12,24,31); sub del_duplicate { my @sorted = sort{$a <=> $b} @_; my @unique; my $current; my $next; foreach my $index (0..@sorted-2) { $current = $sorted[$index]; $next = $sorted[++$index]; if ($current != $next) { push @unique, $current; } } # this is a "fix-it" statement after the loop # in order to get the last element of @sorted push @unique, $next; # add the last value print "@unique\n"; } del_duplicate(@arr); #0 1 10 12 13 15 17 19 24 29 31