my @array1 = (1,2,3,4,5); my @array2 = (4,5,6,7,8); my %counts; my @union = grep ++$counts{$_} == 1, @array1, @array2; my @intersection = grep $counts{$_} == 2, @array1; my @difference = grep $counts{$_} == 1, @array1; my @intersection_c = grep $counts{$_} == 1, @array1, @array2; print "Union: @union\n"; print "Intersection: @intersection\n"; print "Difference: @difference\n"; print "Complement of intersection: @intersection_c\n";