Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How to find and remove duplicate elements from an array?

by psmail (Initiate)
on Jun 21, 2001 at 23:04 UTC ( [id://90493]=perlquestion: print w/replies, xml ) Need Help??

psmail has asked for the wisdom of the Perl Monks concerning the following question: (arrays)

How to find and remove duplicate elements from an array?

Originally posted as a Categorized Question.

  • Comment on How to find and remove duplicate elements from an array?

Replies are listed 'Best First'.
Re: How to find and remove duplicate elements from an array?
by japhy (Canon) on Jun 21, 2001 at 23:13 UTC
      Using hash could help in this effort my @unique= keys %hash; Shashidhar Iddamsetty
Re: How to find and remove duplicate elements from an array?
by jdporter (Paladin) on Jul 20, 2007 at 14:33 UTC
    The answers in the FAQ don't modify the array in-place. In case that's what you need, you can do the following:
    my @a = qw( a a b c c c d e f e f e a f g h h h ); my %seen; for ( my $i = 0; $i <= $#a ; ) { splice @a, --$i, 1 if $seen{$a[$i++]}++; } print "@a\n";
    This can be wrapped in a sub like so:
    sub remove_duplicates(\@) { my $ar = shift; my %seen; for ( my $i = 0; $i <= $#{$ar} ; ) { splice @$ar, --$i, 1 if $seen{$ar->[$i++]}++; } } my @a = qw( a a b c c c d e f e f e a f g h h h ); remove_duplicates( @a ); print "@a\n";
Re: How to find and remove duplicate elements from an array?
by pvaldes (Chaplain) on Nov 19, 2011 at 19:45 UTC
    use Array::Utils::unique:
    use strict; use Array::Utils qw(:all); my @array = qw( a b c d c d e f g g g g g f); my @unique_array = unique(@array); print "@unique_array\n"; __END__ prints: e c a g b d f
    In fact, the code for sub unique is a one liner; you could just hork and use it verbatim:
    @unique_array = keys %{{map { $_ => undef } @array }};
    There's also the (arguably more standard) List::MoreUtils::uniq.

    Its code is nearly a one-liner. Copying it would look like this:

    my %seen; @unique_array = grep { not $seen{$_}++ } @array;
Re: duplicates in an array
by holygrail (Scribe) on Jun 22, 2001 at 18:30 UTC
Re: How to find and remove duplicate elements from an array?
by simmisam (Novice) on Jan 15, 2014 at 19:45 UTC
    A solution that uses Tie::IxHash, a tie-able which preserves the keys of a hash in the order in which they were added:
    use Tie::IxHash; sub removeDuplicates { tie my %tempHash, 'Tie::IxHash'; $tempHash{$_}++ for @_; keys %tempHash; } # Remove repeated elements from an array my @array1 = qw( 4 3 7 4 3 8 9 12 23 43 23 12 ); my @uniqArray = removeDuplicates( @array1 ); print "@uniqArray";
Re: How to find and remove duplicate elements from an array?
by jettero (Monsignor) on Dec 03, 2008 at 12:08 UTC
    { my %seen; @A = grep { !$seen{$_}++ } @A; }

    Originally posted as a Categorized Answer.

Re: How to find and remove duplicate elements from an array?
by msk_0984 (Friar) on Jul 20, 2007 at 07:17 UTC
    Yes this is the most asked questions in Perlmonks.
    use strict; my @arr = ("A", "B", "ABB", "C", "ABB", "B","C" ); my %hash = (); my @list = (); foreach my $word (@arr) { push( @list,$word ) unless $hash{$word}++; } print join("\n", @list), "\n";

    And this can help you out. Even I too was not able to pull out the distinct one from the array. So wat I did was went for "Search" or "Super Search"

    So all of you it is always better to serach for the problem you are facing becos some one or the other has faced these kind of similar problems and have been solved by our respected monks. So please do search before to make any postings.

    Thanks

    Sushil

    Originally posted as a Categorized Answer.

Re: How to find and remove duplicate elements from an array?
by msk_0984 (Friar) on Jul 20, 2007 at 07:19 UTC
    Yes this is the most asked questions in Perlmonks.
    use strict; my @arr = ("A", "B", "ABB", "C", "ABB", "B","C" ); my %hash = (); my @list = (); foreach my $word (@arr) { push( @list,$word ) unless $hash{$word}++; } print join("\n", @list), "\n";

    And this can help you out. Even I too was not able to pull out the distinct one from the array. So wat I did was went for "Search" or "Super Search"

    So all of you it is always better to serach for the problem you are facing becos some one or the other has faced these kind of similar problems and have been solved by our respected monks. So please do search before to make any postings.

    Thanks

    Sushil

    Originally posted as a Categorized Answer.

Re: How to find and remove duplicate elements from an array?
by sankarganesh (Initiate) on Dec 03, 2008 at 11:40 UTC
    { my %hash; $hash{@A} = (); @A = keys %hash; }

    Originally posted as a Categorized Answer.

Re: How to find and remove duplicate elements from an array?
by Fanatic (Sexton) on Nov 20, 2013 at 07:04 UTC
    my @A = (1,2,2,3,4,5); my %hash = (); grep {$hash{$_}++}@A; my @result = keys %hash; print @result;

    Originally posted as a Categorized Answer.

Re: How to find and remove duplicate elements from an array?
by msk_0984 (Friar) on Jul 20, 2007 at 07:15 UTC
    use strict; my @arr = ("A", "B", "ABB", "C", "ABB", "B","C" ); my %hash = (); my @list = (); foreach my $word (@ary) { push( @list,$word ) unless $hash{$item}++; } print join("\n", @list), "\n";

    Originally posted as a Categorized Answer.

Re: How to find and remove duplicate elements from an array?
by lalitbans (Novice) on Feb 01, 2010 at 10:44 UTC
    se Data::Dumper; @brr; @arr = (1,2,3,1); $flag; $len = scalar(@arr); $len = $len - 1; foreach $i(0 .. $len){ $length = scalar(@brr); if($length){ foreach $j(@brr){ if($j == $arr[$i]){ $flag = 'true'; last; }else{ $flag = 'false'; #push(@brr, $arr[$i]); } } if($flag eq 'false'){ push(@brr, $arr[$i]); } }else{ push(@brr, $arr[$i]); } } print Dumper(@brr);

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-07-17 13:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.