If you're trying to find values that appear more than once:
my @seen;
my @dups = grep ++$seen{$_} == 2, @list;
@list = qw( foo bar baz foo foo moo bar );
gives
@dups = qw( foo bar );
If you're trying to put the values that are only found once in @uniq
and values that are found twice or more in @dups
:
my %counts;
++$counts{$_} for @list;
my (@uniq, @dups);
for (@list) {
my $count = $counts{$_};
push @uniq, $_ if $count == 1;
push @dups, $_ if $count == 2;
}
@list = qw( foo bar baz foo foo moo bar );
gives
@uniq = qw( baz moo );
@dups = qw( foo bar );
If you're trying to put the first instance of a value in @uniq
and the second and subsequent values in @dups
:
my (@uniq, @dups, %seen);
push @{ $seen{$_}++ ? \@dups : \@uniq }, $_ for @list;
@list = qw( foo bar baz foo foo moo bar );
gives
@uniq = qw( foo bar baz moo );
@dups = qw( foo foo bar );