my @list = ( a => [ 'b', 'c', 'e' ], b => [ 'd' ], c => [ 'b', 'd' ], f => [ 'a' ], ); print join (',', map { "'$_'" } f2(@list)),"\n"; #### sub f { %r=@_;map{$m{$_}={map{$m{$_}||={};$_=>1}@{$r{$_}}}}keys%r; reverse sort{my$t=$m{$b}{$a}-$m{$a}{$b};warn"$a,$b:$t";$t}keys%m; } #### f,e:0 at ... a,f:0 at ... b,a:1 at ... c,b:1 at ... d,c:1 at ...