my %in_a; my %in_b; undef @in_a{@a}; undef @in_b{@b}; @a = grep {not exists $in_b{$_}} @a; @b = grep {not exists $in_a{$_}} @b;