Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Create unique array --the hard way!

by kcott (Bishop)
on Mar 07, 2014 at 21:11 UTC ( #1077457=note: print w/replies, xml ) Need Help??


in reply to Create unique array --the hard way!

Perhaps this will be suitable (for your class exercise):

#!/usr/bin/env perl -l use strict; use warnings; my @initial = qw{q w e r t y q w e r t y}; print "Initial: @initial"; my $last = ''; my @unique = map { $last eq $_ ? () : ($last = $_) } sort @initial; print "Unique: @unique";

Output:

Initial: q w e r t y q w e r t y Unique: e q r t w y

Update (alternative solution): You could even skip the creation of the unsorted array (your @array_q3):

#!/usr/bin/env perl -l use strict; use warnings; my $last = ''; my @unique = map { $last eq $_ ? () : ($last = $_) } sort map { split +} <DATA>; print "Unique: @unique"; __DATA__ I am supposed to create a unique array I am not supposed to use hashes

Output:

Unique: I a am array create hashes not supposed to unique use

-- Ken

Replies are listed 'Best First'.
Re^2: Create unique array --the hard way!
by Anonymous Monk on Mar 07, 2014 at 21:39 UTC
    Many thanks to all of you guys!!
      I wonder, could this be done using the splice function as well? But how would you use it?

        Well, you could but I don't know why you'd want to.

        In the following script (using Benchmark) you'll see:

        • splice is relatively less efficient. I also suspect it might have future maintainers scratching their heads wondering why it was used ("splice @array, 0, 1" has identical functionality to "shift @array" — it's just slower and requires more keystrokes to code).
        • shift is more efficient than splice.
        • My original map is faster than both of those; it doesn't destroy the original array; and it's a lot less coding.
        #!/usr/bin/env perl use strict; use warnings; use Benchmark qw{cmpthese}; cmpthese -1 => { splice_unique => \&splice_unique, shift_unique => \&shift_unique, map_unique => \&map_unique, }; sub splice_unique { my @sorted = sort qw{q w e r t y q w e r t y}; my $last = ''; my @unique; while (@sorted) { my $element = splice @sorted, 0, 1; if ($last ne $element) { $last = $element; push @unique, $element } } } sub shift_unique { my @sorted = sort qw{q w e r t y q w e r t y}; my $last = ''; my @unique; while (@sorted) { my $element = shift @sorted; if ($last ne $element) { $last = $element; push @unique, $element } } } sub map_unique { my @sorted = sort qw{q w e r t y q w e r t y}; my $last = ''; my @unique = map { $last eq $_ ? () : ($last = $_) } @sorted; }

        Representative result:

        Rate splice_unique shift_unique map_unique splice_unique 93699/s -- -5% -20% shift_unique 98642/s 5% -- -16% map_unique 117028/s 25% 19% --

        -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1077457]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2020-08-09 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (54 votes). Check out past polls.

    Notices?