#! perl -slw use strict; sub pairs { map { my $i = $_; map [ @_[ $i, $_ ] ], $i+1 .. $#_; } 0 .. $#_; } my %pairCounts; ++$pairCounts{ "@$_" } for map pairs( sort split ), ; print "@$_" while @{ $_ = [ each %pairCounts ] }; __DATA__ dog monkey cat cat ball stone monkey iron cat zoo