Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Removing Duplicate Array Elements!!!

by dnickel (Novice)
on Sep 27, 2001 at 17:03 UTC ( #115078=perlquestion: print w/ replies, xml ) Need Help??
dnickel has asked for the wisdom of the Perl Monks concerning the following question:

I have an array that contains two of every element. I need to remove the duplicate of every element so that I may have just one of each. Any advice will be much appreciated. Thanks

Comment on Removing Duplicate Array Elements!!!
Re: Removing Duplicate Array Elements!!!
by physi (Friar) on Sep 27, 2001 at 17:09 UTC
    You can use a temporary hash to filter out the doubles:
    use strict; my @a= (1,2,3,4,5,6,6,5,4,3,2,1); my %help; map { $help{$_}=1} @a; @a = keys %help; print @a;
    ----------------------------------- --the good, the bad and the physi-- -----------------------------------

      While using map certainly works, using map in a void context like you do is inefficient. Map returns an array for you regardless of whether you use it or not. As a result using map as a surrogate for loop is bad practice. In golf you use it because it is 2 chars shorter than a for loop. That is about the only (valid) reason to use it in a void context :-) All you actually want is:

      $help{$_}++ for @a

      This is far more efficient, it is also shorter for what it's worth. Thanks to chipmunk for straigtening me out on this some months ago.

      my @a= (1,2,3,4,5,6,6,5,4,3,2,1); my %help; my @wasted_array = map { $help{$_}=1 } @a; @a = keys %help; print @wasted_array, "\n", @a;




        If you are looking an efficient method to do this then try.

        my %hash;
        @hash{@a} = ();
        @a = keys %hash;

        But that only works, just as your example, for non-references as the keys of a hash can only be strings. If @a may contain references that you want to preserve then use

        my %hash;
        @hash{@a} = @a;
        @a = values %hash;

        This is slightly less efficient as it causes an extra copy of each element in @a

        I generally write this as

        @a = do { my %h; @h{@a} = @a; values %h }; # unique
Re: Removing Duplicate Array Elements!!!
by tachyon (Chancellor) on Sep 27, 2001 at 17:18 UTC

    You can do it like this using a hash:

    sub remove_dups { my %hash; $hash{$_}++ for @_; return keys %hash; } @ary = remove_dups( @ary )

    This is a very common question. In fact it was asked yesterday - have a look at my answer at Re: A C-like brain in a Perl-like world - particularly the bit about the Perl FAQs - they will save you heaps of time. Virtually every common problem is covered in the FAQs so they are well worth a look.




Re: Removing Duplicate Array Elements!!!
by davorg (Chancellor) on Sep 27, 2001 at 17:18 UTC
Re: Removing Duplicate Array Elements!!!
by dnickel (Novice) on Sep 27, 2001 at 23:01 UTC
    Thanks for all the help...problem solved!!! How do I rate responses?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://115078]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-10-22 23:08 GMT
Find Nodes?
    Voting Booth?

    For retirement, I am banking on:

    Results (122 votes), past polls