Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Duplicate Element in an Array

by vihnny (Initiate)
on Aug 21, 2012 at 20:50 UTC ( #988859=perlquestion: print w/replies, xml ) Need Help??
vihnny has asked for the wisdom of the Perl Monks concerning the following question:


I gotta a question for you. If I had inputs entered by the user and I want to display them back but separately, how would I do it?


The user enter these numbers: (1200, 1300, 1200, 1000, 1100, 1200, 1500, 1700, 2000, 2100, 3000, 2100, 1200, 1500, 1700, 1700) So I insert them in the array @input;

now I want to separate its elements if they are duplicate and create new arrays and put these items in there. For each element duplicated I create a new array and place that element in there. Example:

group one (1200, 1300, 1000, 1100, 1500, 1700, 2000, 2100, 3000)

group two (1200, 2100)

group three (1200, 1500, 1700)

group four (1200, 1700)

How can I code this?

Replies are listed 'Best First'.
Re: Duplicate Element in an Array
by johngg (Abbot) on Aug 21, 2012 at 23:56 UTC

    It is not clear how you arrive at your expected output. Your group one is clear enough but I can't see the logic of how you arrive at your groups two, three and four. Perhaps you could explain further how you are separating the items. Here is a possible solution that builds an AoA by forming arrays of unique and duplicate items then recursively acting on the duplicates until none are left. It does not arrive at the same group members you show. The code.

    use strict; use warnings; use Data::Dumper; my @input = qw{ 1200 1300 1200 1000 1100 1200 1500 1700 2000 2100 3000 2100 1200 1500 1700 1700 }; my @groups = deDup( \ @input ); print Data::Dumper->Dumpxs( [ \ @groups ], [ qw{ *groups } ] ); sub deDup { my $raToCheck = shift; my $raUniq = []; my $raDups = []; my %seen; push @{ $seen{ $_ } ++ ? $raDups : $raUniq }, $_ for @$raToCheck; return $raUniq, @$raDups ? deDup( $raDups ) : (); }

    The output.

    @groups = ( [ '1200', '1300', '1000', '1100', '1500', '1700', '2000', '2100', '3000' ], [ '1200', '2100', '1500', '1700' ], [ '1200', '1700' ], [ '1200' ] );

    I hope this is helpful.



Re: Duplicate Element in an Array
by choroba (Chancellor) on Aug 21, 2012 at 21:17 UTC
    I would probably proceed like this:
    Create a hash. The numbers will be the keys, the numbers of occurrences will be the values. Once the input is finished, I would create another hash, the number of occurences being the keys, and (references to) the lists of numbers being the values.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Duplicate Element in an Array
by nemesdani (Friar) on Aug 21, 2012 at 21:02 UTC
    First and most importatly, try it.
    If I understand your problem well enough ( which I'm not sure of, sorry), try to use hashes instead of arrays. If the imput number doesn't exist in the hash as a key you create it, if it does, you create another hash with the given key. Values are not important.
    Tiresome. Maybe an expert has a better solution, but it would be useful to know, what will you use these arrays or hashes for.

    I'm too lazy to be proud of being impatient.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://988859]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2017-08-20 11:10 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (315 votes). Check out past polls.