$ cat lists.pl #!/usr/bin/perl use strict; use warnings; use 5.10.0; # Build a few lists my @lists; for my $i (0 .. 1+int(3*rand)) { # several lists to compare push @{$lists[$i]}, int 16 * rand for 1 .. 15; print "LIST $i: ", join(", ", sort {$a<=>$b} @{$lists[$i]}), "\n"; } # Build a scoreboard of which items are in each list my %tally; my $bit=0; for my $list (@lists) { for my $item (@$list) { $tally{$item} |= 1<<$bit; } ++$bit; } # Show which items were in all lists my $all = (1<<$bit)-1; print "There were $bit lists. Items found:\n"; for my $item (sort {$a<=>$b} keys %tally) { print "Item $item: "; if ($tally{$item} == $all) { print "in *all* lists\n"; next; } my @present; for ($bit=0; $bit<@lists; $bit++) { push @present, $bit if $tally{$item} & 1<<$bit; } print "in lists: ", join(", ", @present), "\n"; } $ perl lists.pl LIST 0: 1, 3, 4, 6, 8, 8, 9, 9, 10, 10, 11, 11, 11, 13, 14 LIST 1: 0, 0, 0, 0, 0, 3, 3, 3, 5, 6, 8, 11, 11, 15, 15 LIST 2: 0, 1, 3, 4, 6, 6, 6, 8, 8, 9, 9, 9, 12, 13, 15 LIST 3: 1, 4, 4, 5, 5, 6, 8, 8, 8, 8, 9, 9, 12, 12, 13 There were 4 lists. Items found: Item 0: in lists: 1, 2 Item 1: in lists: 0, 2, 3 Item 3: in lists: 0, 1, 2 Item 4: in lists: 0, 2, 3 Item 5: in lists: 1, 3 Item 6: in *all* lists Item 8: in *all* lists Item 9: in lists: 0, 2, 3 Item 10: in lists: 0 Item 11: in lists: 0, 1 Item 12: in lists: 2, 3 Item 13: in lists: 0, 2, 3 Item 14: in lists: 0 Item 15: in lists: 1, 2