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

How can I delete all the values in an array that have a duplicate value i.e. eliminate the duplicate value(s), and the original, keeping only the values that appeare once in the array ?

for example, if my array contains the following:

A1, B2, C3, C3, D4

I would want the resulting array to contain this:

A1, B2, D4

Any insight would be greatly appreciated

  • Comment on Eliminate duplicate and original values

Replies are listed 'Best First'.
Re: Eliminate duplicate and original values
by guha (Priest) on Nov 25, 2001 at 23:26 UTC
    There is of course an "enormous" potential for golfing the snippet below
    foreach (@array) { $hash{$_}++; } foreach (keys %hash) { delete $hash{$_} if ($hash{$_} > 1); } @array2 = keys %hash;

    HTH

      More compactly as,
      $hash{$_}++ foreach (@array); @array2 = grep( ($hash{$_} == 1), keys %hash );
        I came up with this, at 46 chars:
        @array=do{grep$_{$_}<2,grep{!$_{$_}++}@array};
        Of course, if we were truely golfing, that comes out at:
        sub find_duplicates { #23456789_123456789_123456789_ grep$_{$_}<2,grep{!$_{$_}++}@_ }
        30 characters :)
Re: Eliminate duplicate and original values
by DrManhattan (Chaplain) on Nov 25, 2001 at 23:31 UTC
    Here's a straightforward solution
    #!/usr/bin/perl use strict; my @input = qw(a b c d e d e f); # %seen is a hash used to count the number of times # an element appears in the array. @result contains # the results of removing the duplicates. my (%seen, @result); foreach (@input) { $seen{$_}++; } foreach (@input) { # Snag the elements which appear only once. # Use the original elements of the input array # as hash keys rather than keys(%seen) to # preserve ordering if ($seen{$_} == 1) { push @result, $_; } }

    -Matt

Re: Eliminate duplicate and original values
by premchai21 (Curate) on Nov 25, 2001 at 23:13 UTC
    What about if the array contains 1, 2, 3, 2, 4? Would you like the result to be 1, 2, 3, 2, 4? Or 1, 3, 4? That is, what exactly counts as a duplicate value?