<p>Given:</p>

<pre><code>my @mylist1;
push(@mylist1,"A");
push(@mylist1,"B");
push(@mylist1,"C");

my @mylist2;
push(@mylist2,"A");
push(@mylist2,"D");
push(@mylist2,"E");
</code></pre>

<p>What's the quickest way in Perl to insert in mylist2 all elements that are in mylist1 and not already in mylist2 (ABCDE). </p>

```
```
my %k;
map { $k{$_} = 1 } @mylist1;
map { $k{$_} = 1 } @mylist2;
@mylist2 = keys %k;
```
```
Alternatively:
```
```
my %k;
map { $k{$_} = 1 } @mylist2;
push(@mylist2, grep { !exists $k{$_} } @mylist1);
```
```
Actually - these might be wrong because they don't account for whether duplicates might exist in either of the original lists.
You didn't say in your question whether the lists are supposed to represent sets (which can't contain duplicates) or just plain lists.  That you effectively want
```
@mylist2 = @mylist1 U @mylist2
```
suggests that you are treating them as sets.
EDIT: changed increment to assign - saves a read of the hash value