in reply to Eliminate duplicate and original values

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

Replies are listed 'Best First'.
Re: Re: Eliminate duplicate and original values
by data64 (Chaplain) on Nov 25, 2001 at 23:56 UTC
    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 :)
        Eliminating the hashes chops it down to 24, but of course it won't run with strict refs in use.
        #23456789_123456789_1234 grep$$_<2,grep{!$$_++}@_

        -Matt

        This does not eliminate the duplicate elements. See the requirements in the root node.
        If the original array contains A1, B2, C3, C3, D4, then the output should contain A1, B2, D4.
        C3 should not be included.
        That's a very clever way of obtaining unique values though and will get copied in other places, if you don't mind. :)