toddprof has asked for the wisdom of the Perl Monks concerning the following question:

0

Originally posted as a Categorized Question.

  • Comment on How do I find if an array has duplicate elements, if so discard it?

Replies are listed 'Best First'.
Re: How do I find if an array has duplicate elements, if so discard it?
by DamnDirtyApe (Curate) on Jul 13, 2002 at 20:14 UTC
    my @old_arr = ( 'foo', 'bar', 'zoot', 'boo', 'bar', 'zoot' ) ; my %hash = map { $_ => 1 } @old_arr ; my @new_arr = keys %hash ; my $dups_found = @old_arr - @new_arr ;

    Originally posted as a Categorized Answer.

Re: How do I find if an array has duplicate elements, if so discard it?
by BrowserUk (Patriarch) on Jul 13, 2002 at 21:19 UTC

    Another way.....in constant (long) time.

    #! perl -w use Quantum::Superpositions; use strict; local ($,=", ", $\="\n"); my @ary = qw/ 1 2 3 3 4 5 6 6 7 8 9/; my @unique = eigenstates(any(@ary)); print @ary; print @unique; my @dStrings = qw/ alpha alpha bravo charlie delta echo foxtrot charli +e golf golf golf hotel india/; my @uStrings = eigenstates(any(@dStrings)); print @dStrings; print @uStrings; my @chars = eigenstates(any(split //, "the quick brown fox jumps over +the lazy dog")); print sort @chars; __DATA__ C:\test>181519 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9 1, 2, 3, 4, 5, 6, 7, 8, 9 alpha, alpha, bravo, charlie, delta, echo, foxtrot, charlie, golf, gol +f, golf, hotel, india bravo, charlie, india, golf, delta, hotel, alpha, echo, foxtrot , a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w +, x, y, z

    Originally posted as a Categorized Answer.