# FAQ 4.43 How do I compute the difference of two arrays? How do I compute the intersection...

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.43: How do I compute the difference of two arrays?  How do I compute the
intersection of two arrays?

Use a hash. Here's code to do both and more. It assumes that each
element is unique in a given array:

@union = @intersection = @difference = ();
%count = ();
foreach \$element (@array1, @array2) { \$count++ }
foreach \$element (keys %count) {
push @union, \$element;
push @{ \$count > 1 ? \@intersection : \@difference
}, \$element;
}

Note that this is the *symmetric difference*, that is, all elements in
either A or in B but not in both. Think of it as an xor operation.

--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

## Update for difference of two arrays (was: FAQ 4.43 How do I compute the difference of two arrays? How do I compute the intersection of two arrays?)

^^^
Of course it assumes each element is unique in a given array. I breaks if its
not (ie: dups).

^^^
What in the hell does this mean?

I'm so tired of seeing this flawwed logic. Can't we just fix this once and for
all
FAQ workers ???

This is the real solution, read carefully:

------------------------------------------------------
@union = @intersection = @difference = ();
%count = ();
foreach \$element (@array1) { \$count = 1 }
foreach \$element (@array2) { \$count |= 2 }
foreach \$element (keys %count) {
push @union, \$element;
push @{ \$count == 3 ? \@intersection : \@difference }, \$element;
}
-------------------------------------------------------

This fixes the "unique element" major, major drawback. Imagine the assumption
that
there are no duplicates in an array ? What the hell is that ???

The bitwise OR is just as fast as increment. If its one extra assembly step,
so what ?  It mitigates in-array dup's.

The other fallicy in this FAQ is the @difference array. Different than what
exactly ?  Each other ?  What the hell does that tell you ???  Nothing !!!

The proof is below. I challenge anybody to make the argument for this standing
FAQ -> NOT TO BE CHANGED/FIXED once and for all !!!

sln

---------------------------------------------------------
use strict;
use warnings;

my @array1 = (1,3,3,1,5,5,7,9,100,200,300,100);
my @array2 = (0,2,4,6,2,6,0,8,100,200,300);

my (\$element,%count,@union,@intersection,@difference);

@union = @intersection = @difference = ();
foreach \$element (@array1) { \$count = 1 }
foreach \$element (@array2) { \$count |= 2 }
foreach \$element (keys %count) {
push @union, \$element;
push @{ \$count == 3 ? \@intersection : \@difference }, \$element;
}

@union        = sort @union;
@intersection = sort @intersection;
@difference   = sort @difference;

print "\n";
print "Array1       = @array1\n";
print "Array2       = @array2\n";
print "union        = @union\n";
print "intersection = @intersection\n";
print "difference   = @difference\n";

__END__

Output:
c:\temp>perl jjj.pl

Array1       = 1 3 3 1 5 5 7 9 100 200 300 100
Array2       = 0 2 4 6 2 6 0 8 100 200 300
union        = 0 1 2 3 4 5 6 7 8 9 100 200 300
intersection = 100 200 300
difference   = 0 1 2 3 4 5 6 7 8 9

c:\temp>