in reply to Print keys corresponding to duplicate values

This solution inverts the hash like this:

k1 v1 k2 v2 k3 v1 v1 v2 (a => 1, b => 2, c => 1) => (1 => [a,c], 2 => [b])

Then it just becomes a matter of finding those value-keys who point to more than a single original key name.

use strict; use warnings; my %hash = (a => 1, b => 2, c => 1, d => 3, e => 5); my @non_unique = duplicated_values_keys(%hash); print "$_\n" for @non_unique; sub duplicated_values_keys { my %hash = @_; my %inverted; push @{$inverted{$hash{$_}}}, $_ for keys %hash; return map {@{$inverted{$_}} > 1 ? @{$inverted{$_}} : ()} keys %in +verted; }

Outputs:

a c

...in no particular order.


Dave