http://www.perlmonks.org?node_id=181536


in reply to How do I find if an array has duplicate elements, if so discard it?

use strict ; use warnings ; use Data::Dumper ; sub has_dups { my $arr = shift ; my %counter ; foreach ( @$arr ) { return 1 if $counter{$_}++ ; } return 0 ; } my @A = (1, 2, 3, 4, 5) ; my @B = (6, 7, 7, 8, 9) ; my @C = (10, 11, 12, 13, 15) ; my @D = grep { !has_dups($_) } ( \@A, \@B, \@C ) ; print Dumper( \@D ) ;
  • Comment on Re: How do I find if an array has duplicate elements, if so discard it?
  • Download Code

Replies are listed 'Best First'.
Re: Answer: Clarify: How do I find if an array has duplicate elements, if so discard it?
by ysth (Canon) on Nov 07, 2003 at 10:11 UTC
    I took a look at this and wondered what on earth it was trying to do. Then I reread the question. This answer does try to provide a meaningful example of detecting an array with duplicate elements and discarding it. But I have a feeling the questioner meant to say "if so discard" them.

    If you are downvoting this for other than lack of interest, thanks for sending me a message or reply to let me know why so I can do better (or just stay quiet) next time.