#!/usr/bin/perl use warnings; use strict; my @sets = ([ 0, 1, 2, 3, 7, 8, 9, 10, 11, 12, 13, 14, 15, ], [ 0, 3, 7, 8, 10, 11, 13, 14, 16, ], [ 0, 1, 2, 3, 5, 7, 8, 10, 11, 12, 13, 14, 16, ], [ 2, 3, 4, 6, 7, 8, 10, 11, 12, 14, 16, ], [ 0, 1, 2, 3, 7, 8, 9, 10, 11, 13, 14, 15, ]); my $numsets = @sets; my %intersection; @intersection{ @{$sets[0]} } = (); for ( @sets[1..$#sets] ) { my %this; @this{ @$_ } = (); delete @intersection{ grep { ! exists $this{$_) } keys %intersection}; } my @intersection = sort {$a <=> $b} keys %intersection; print "Common items in the $numsets sets: @intersection\n"; __END__ prints: Common items in the 5 sets: 3 7 8 10 11 14