extracting duplicating entries in a separate list

S

Sherm Pendley

I have an array

@myarray = (2, 3, 5, 4, 3, 6, 3, 2, 1,0);
I want to separate the duplicate entries

perldoc -q duplicate

How can I remove duplicate elements from a list or array?

sherm--
 
S

swamyb

I have an array

@myarray = (2, 3, 5, 4, 3, 6, 3, 2, 1,0);
I want to separate the duplicate entries and put in a list and put all
the unique in other list.

list 1 contains (2,2,3,3,3)
list 2 contains (5,4,6,1,0).


Can you please help me?

thanks
Swamy
 
A

Arne Ruhnau

I have an array

@myarray = (2, 3, 5, 4, 3, 6, 3, 2, 1,0);
I want to separate the duplicate entries and put in a list and put all
the unique in other list.

list 1 contains (2,2,3,3,3)
list 2 contains (5,4,6,1,0).

You want to use a hash to count the occurences of each element in @myarray.
You then want to iterate over this hash and, depending on the count of the
current key, either stuff it in list 1 (count times), or in list 2 (count
times).

I wonder whether this is a FAQ... and well, sort of. A look at

perldoc -q duplicate

could have provided you with the very same idea.

Arne Ruhnau
 
S

swamyb

Hi

Thanks. I looked at perldoc -q duplicate. It only shows how to remove
duplicate and
not separate.

the duplicate logic only removes second or third occurences, but still
contain first occurence.
That is not what I wanted.


thanks
Swamy
 
G

Gunnar Hjalmarsson

I have an array

@myarray = (2, 3, 5, 4, 3, 6, 3, 2, 1,0);
I want to separate the duplicate entries and put in a list and put all
the unique in other list.

list 1 contains (2,2,3,3,3)
list 2 contains (5,4,6,1,0).

my %saw;
$saw{$_}++ for @myarray;
my @duplicates =
map { $saw{ $myarray[$_] } > 1 ? splice @myarray, $_, 1 : () }
reverse 0..$#myarray;
 
A

A. Sinan Unur

I have an array

@myarray = (2, 3, 5, 4, 3, 6, 3, 2, 1,0);
I want to separate the duplicate entries and put in a list and put
all the unique in other list.

list 1 contains (2,2,3,3,3)
list 2 contains (5,4,6,1,0).

my %saw;
$saw{$_}++ for @myarray;
my @duplicates =
map { $saw{ $myarray[$_] } > 1 ? splice @myarray, $_, 1 : () }
reverse 0..$#myarray;

I am not sure splice and reverse are necessary here :)

#!/usr/bin/perl

use strict;
use warnings;

my @myarray = (2, 3, 5, 4, 3, 6, 3, 2, 1,0);

my %saw;
$saw{$_}++ for @myarray;

my @unique = grep { $saw{$_} == 1 } @myarray;
my @duplicates = grep { $saw{$_} > 1 } @myarray;

print "Unique = @unique\n";
print "Duplicates = @duplicates\n";

__END__

Sinan
 
G

Gunnar Hjalmarsson

A. Sinan Unur said:
Gunnar said:
I have an array

@myarray = (2, 3, 5, 4, 3, 6, 3, 2, 1,0);
I want to separate the duplicate entries and put in a list and put
all the unique in other list.

list 1 contains (2,2,3,3,3)
list 2 contains (5,4,6,1,0).

my %saw;
$saw{$_}++ for @myarray;
my @duplicates =
map { $saw{ $myarray[$_] } > 1 ? splice @myarray, $_, 1 : () }
reverse 0..$#myarray;

I am not sure splice and reverse are necessary here :)

my @unique = grep { $saw{$_} == 1 } @myarray;
my @duplicates = grep { $saw{$_} > 1 } @myarray;

What?? Thought you liked reverse(). ;-)
 
F

Fabian Pilkowski

I have an array

@myarray = (2, 3, 5, 4, 3, 6, 3, 2, 1,0);
I want to separate the duplicate entries and put in a list and put all
the unique in other list.

list 1 contains (2,2,3,3,3)
list 2 contains (5,4,6,1,0).

Could you explain why your list 1 is sorted and list 2 is in the
original order (not sorted). Is this really what you want?

If order does matter you could do something like:


my( @singles, @doubles, %count );
$count{$_}++ for @myarray;
for ( @myarray ) {
if ( $count{$_} == 1 ) {
push @singles, $_;
}
else {
push @doubles, $_;
}
}
print "singles: @singles\n";
print "doubles: @doubles\n";


If order doesn't matter it doesn't matter to keeping the order.

Btw, I've tried to type less code for this task, but all my solutions
needs the same number of characters. Perhaps someone else wants to play
golf ;-)

my @a = qw/ 2 3 5 4 3 6 3 2 1 0 /;
my(@s,@d);

push@{$_{$_}-1?\@d:\@s},$_,for+grep++$_{$_},@a;
# map{push@{$_{$_}-1?\@d:\@s},$_}grep++$_{$_},@a;
# ++$_{$_}for@a;push@{$_{$_}-1?\@d:\@s},$_,for@a;

print "singles: @s\ndoubles: @d\n";

regards,
fabian
 
A

Anno Siegel

A. Sinan Unur said:
I have an array

@myarray = (2, 3, 5, 4, 3, 6, 3, 2, 1,0);
I want to separate the duplicate entries and put in a list and put
all the unique in other list.

list 1 contains (2,2,3,3,3)
list 2 contains (5,4,6,1,0).

my %saw;
$saw{$_}++ for @myarray;
my @duplicates =
map { $saw{ $myarray[$_] } > 1 ? splice @myarray, $_, 1 : () }
reverse 0..$#myarray;

I am not sure splice and reverse are necessary here :)

#!/usr/bin/perl

use strict;
use warnings;

my @myarray = (2, 3, 5, 4, 3, 6, 3, 2, 1,0);

my %saw;
$saw{$_}++ for @myarray;

my @unique = grep { $saw{$_} == 1 } @myarray;
my @duplicates = grep { $saw{$_} > 1 } @myarray;

Or even

my ( @duplicates, @unique);
push @{ $saw{ $_} > 1 ? \ @duplicates : \ @unique}, $_ for @myarray;

Anno
 
T

Tad McClellan

A. Sinan Unur said:
You don't understand, I am very possesive :) :)


Then you must prefer using my() over our()?

Your possesiveness may lead to sub-optimal code in that case. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top