Evolution of a solution

Combing code, I found I have used these 5 different solutions to a common task for my work:
   A command returns an unordered list of integers, with some duplicates.
   Remove duplicates (after sorting the list).
   (Listed below from older to more recent):

mkmcconn

#!/usr/bin/perl -w use strict; use Benchmark; my $n = 10000; my $o = rand($n); my @rray = (1..$n,$o..$n); # make an array and pollute it # with duplicates my @array = (); @array = sort {$a <=> $b} @rray; my %tests = ( totemp => sub { my @newarray = (); my $i = 0; for ( @array ){ my $temp = $array[ ++$i ] ? $array[ $i ]: ""; push @newarray, $_ unless $_ eq $temp; } }, ternary => sub { my @newarray = (); my $i = 0; for ( @array ){ push @newarray, $_ unless $_ eq $array[ $array[ $i+1 ] ? ++$ +i : 0 ]; } }, toend => sub { my @newarray = (); my $i = 0; for my $list (@array){ if ( $array[ ++$i ] ){ push @newarray, $list unless $list eq $array[ $i ]; }else{ push @newarray,$array[-1]; } } }, cookbook => sub { my @newarray = (); my %duplicates = (); @newarray = grep { ! $duplicates{$_} ++ } @array; }, modulus => sub { my @newarray = (); for (0..(@array-2)){ $array[$_] % $array[$_+1] and push @newarray, $array[$_]; } push @newarray, $array[-1]; }, ); timethese ( shift @ARGV || -5, \%tests);

Update:
Thanks chipmunk
ternary is a bizarre solution - and as you discovered (and I did not realize, but it's obvious looking at it now), it breaks completely when there's a 0 in the array! The original was echo'd from the commandline, and appears to have been used to remove duplicates from a list of words.
modulus also works as long as there is no zero in the list. If there is more than one zero, it will not run. If there is one zero, it will be skipped.
Thanks for taking time to comment, chipmunk. Some small adjustments made in the post, to prevent complete failure of the tests.


In reply to remove duplicates from array by mkmcconn

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.