Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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!!!
Replies are listed 'Best First'.
Re: Removing Duplicate Array Elements!!!
by davorg (Chancellor) on Sep 27, 2001 at 17:18 UTC
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;

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        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.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

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?
      my @dup_array = qw\sridhar sridhar guru chan guru hubli hubli hubli hu +rryo\; my %unique_hash; foreach my $x (@dup_array) { $unique_hash{$x} = 1; } @dup_array = keys %unique_hash; print "\n\n\n@dup_array";
      Here is efficient way to remove dup elements in array, Every element is stored in a hash with key as the array element and value as 1. Since a hash cannot have duplicate keys, after populating the hash, the keys from the hash will consist of distinct array elements.

        Hello SridharKesari, and welcome to the Monastery!

        The hash method has one drawback: it doesn’t preserve the order in which the words first appear in the original array. Here’s a simpler approach that does:

        #! perl use strict; use warnings; use Data::Dump; use List::MoreUtils 'uniq'; dd uniq qw{ sridhar sridhar guru chan guru hubli hubli hubli hurryo };

        Output:

        22:48 >perl 1381_SoPW.pl ("sridhar", "guru", "chan", "hubli", "hurryo") 22:48 >

        And here’s the sorted equivalent:

        dd sort +uniq qw{ sridhar sridhar guru chan guru hubli hubli hubli hur +ryo };

        (The + is needed here to tell Perl that uniq supplies the values to be sorted, not the sort comparison routine. See the section beginning “Warning: syntactical care is required...” in sort.)

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        my @dup_array = qw\sridhar sridhar guru chan guru hubli hubli hubli hu +rryo\; my %unique_hash; foreach my $x (@dup_array) { $unique_hash{$x} = 1; } @dup_array = keys %unique_hash; print "\n\n\n@dup_array";

        Sridar, I love you as a fellow human being, but please use <code> </code> HTML tags around your code. You are killing me.

        I see choroba's note, but I can't help it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2016-05-31 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?