Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How to find and remove duplicate elements from an array?

( #90493=categorized question: print w/ replies, xml ) Need Help??
Contributed by psmail on Jun 21, 2001 at 23:04 UTC
Q&A  > arrays


Answer: How to find and remove duplicate elements from an array?
contributed by japhy

This is a FAQ, because it's so frequently asked. See How can I remove duplicate elements from a list or array?

Answer: How to find and remove duplicate elements from an array?
contributed by jdporter

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";
Answer: How to find and remove duplicate elements from an array?
contributed by pvaldes

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;
Answer: How to find and remove duplicate elements from an array?
contributed by simmisam

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";

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (9)
    As of 2015-07-07 00:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (86 votes), past polls