#!/usr/bin/perl use strict; use warnings; my @x = qw (hi there hello there hello world); my @y = qw (hi there hello new element there there); my (%xh,%yh) = (); # count the number of repeats in each array $xh{$_}++ for (@x); $yh{$_}++ for (@y); my $common = 0; # for all keys in the hash check if it exists in the other hash. if it does then choose the min of the two for number of repeats. for (keys %xh) { $common += min($xh{$_}, $yh{$_}) if (exists($yh{$_})); } print "Number common = $common\n"; sub min { return (($_[0] <= $_[1]) ? $_[0] : $_[1]); }