Monday, June 30, 2008

Perl: Remove duplicate elements from list


Share at Facebook

Today I'm going to provide few examples that guide you how to remove the duplicate elements from any given array in Perl.

I'm using the sorting method that sort the array first using sort() function of Perl. Then just loop the array and take the unique elements into another array. I'm using sort, cause this will make easy to track the uniqueness, since you know after 10 there won't be any 9 since 9 is less than 10.

Here is one example. The function remove_duplicate_from_array() takes an array as a parameter, and after removing the duplicates from the array, it returns the new array containing only the unique elements.


## This function takes the array as parameter
## Returns the array that contains the unique elements
sub remove_duplicate_from_array{
my @lists = @_;

## The array holds all the unique elements from list
my @list_unique = ();

## Initial checker to remove duplicate
my $checker = -12345645312;

## For each sorted elements from the array
foreach my $list( sort( @lists ) ){

## move to next element if same
if($checker == $list){
next;
}

## replace old one with new found value
else{
$checker = $list;
push( @list_unique, $checker);
}
}

## Finally returns the array that contains unique elements
return @list_unique;
}
To use the function here is an example code that just call the function using an array and prints the unique elements on screen.

## Initial list/array
my @list = qw(3 1 2 1 3 4 1 5 1 6 8 1 2 1 2 7 1 1 2 3 4 6 1);

## pass the reference of array and get the reference of unique lists
my @list = remove_duplicate_from_array(@list);
foreach my $val(@list){
print "$val\n";
}
Now you may think passing an array and returning another array might be memory consuming and slow. In that case you can use array reference passing. Though in Perl passing an array is not that much slow like other languages.

Here is the modified function of above one. This one takes array reference as parameter, and returns another array reference that contains the unique elements.
## This function takes the array reference
## Returns the reference of array after removing the duplicate
sub remove_duplicate_from_array{
my $ref_list = shift;

## The array holds all the unique elements from list
my @list_unique = ();

## Initial checker to remove duplicate
my $checker = -12345645312;

## For each sorted elements from the array
foreach my $list( sort( @$ref_list ) ){

## move to next element if same
if($checker == $list){
next;
}

## replace old one with new found value
else{
$checker = $list;
push( @list_unique, $checker);
}
}

## Finally returns the reference of array
return \@list_unique;
}
To use the function below is the code block that call the function and print the unique elements into the screen.
## Initial list/array
my @list = qw(3 1 2 1 3 4 1 5 1 6 8 1 2 1 2 7 1 1 2 3 4 6 1);

## pass the reference of array and get the reference of unique lists
my $ref_list = remove_duplicate_from_array(\@list);
foreach my $val(@$ref_list){
print "$val\n";
}
## don't forget to free the meomory :)
undef($ref_list);
Hope this discussion will aid you to code in Perl more easily. If you are interested in Perl, you can take a look at other Perl related topics at http://icfun.blogspot.com/search/label/perl,Any comment or idea, just let me know.




2 comments:

Anonymous said...

Thank you! Was looking for a lot of "remove duplicate" functions and yours are great and works for me!

Anonymous said...

Your code is not only buggy, but also excessively long. You can do it in three code lines:

my @arr = ( 1, 4, 2, 2, 3 );
my %tmp = map { $_, 0 } @arr;
@arr = sort(keys %tmp);