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

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

#!/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


In reply to Re^4: Create unique array --the hard way! by kcott
in thread Create unique array --the hard way! by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.