# Find what is in array1 and not in array2

Discussion in 'Perl Misc' started by The Poor, Sep 26, 2003.

1. ### The PoorGuest

In FAQ,
+
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{\$element}++ }
foreach \$element (keys %count) {
push @union, \$element;
push @{ \$count{\$element} > 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.

-
But it is not what I need. I need to find what in array1, but not in
array2. Can anyone show me how to do that?
If I just juse array1-union, then should that be the what I need? What
is the running time will be? 2(n+m)?
Currently I am using 2 for loops to compare anything in 1 to anything
in 2, that makes the running time n*m/2

The Poor, Sep 26, 2003

2. ### Kiel R StirlingGuest

John Bokma <> wrote:
>The Poor wrote:
>
>> But it is not what I need. I need to find what in array1, but not in
>> array2. Can anyone show me how to do that?

>
>my %hash;
>@hash2{ @array2 } = ();
>
>my @notin2;
>
>foreach my \$el (@array1) {
>
> push(@notin2, \$el) unless exists \$hash2{\$el}; # O(1) look up
>}
>
>running time is O(max(n,m))
>
>--
>Kind regards, virtual home: http://johnbokma.com/ ICQ: 218175426
> web site hints: http://johnbokma.com/websitedesign/
>John I count my toes ~ one to ten ~ I meditate ~ and feel the Zen
>

Why not use grep ??

#!/usr/bin/perl -w
use strict;

my @a = qw(1 2 3 4);
my @b = qw(2 4);

foreach my \$v (@a) {
print \$v unless grep /^\$v\$/, @b;

}

-Kiel R Stirling

Kiel R Stirling, Sep 26, 2003

3. ### James E KeenanGuest

"The Poor" <.2y.net> wrote in message
news:...
> In FAQ,
> +
> How do I compute the difference of two arrays? How do I compute the
> intersection of two arrays?
> [snip]
> 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.
>
> -
> But it is not what I need. I need to find what in array1, but not in
> array2. Can anyone show me how to do that?

[snip]

If you would like a simple object-oriented interface to a solution to this
problem, consider my CPAN module List::Compare
(http://search.cpan.org/author/JKEENAN/List-Compare-0.2/Compare.pm)

my \$lc = List::Compare->new(\@array1, \@array2);
my @unique_to_first = \$lc->get_unique;

HTH

Jim Keenan

James E Keenan, Sep 27, 2003
4. ### Eric J. RoodeGuest

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

.2y.net (The Poor) wrote in news:30bcc0c9.0309260752.1d1e44b9

> But it is not what I need. I need to find what in array1, but not in
> array2. Can anyone show me how to do that?
> If I just juse array1-union, then should that be the what I need? What
> is the running time will be? 2(n+m)?
> Currently I am using 2 for loops to compare anything in 1 to anything
> in 2, that makes the running time n*m/2

Use a temporary hash:

my %temphash;
@temphash{@array1} = ();
delete @temphash{@array2};
my @in_1_but_not_in_2 = keys %temphash;

I'm not sure how to compute the runtime; my guess is that it'd be O(m+n).

- --
Eric
\$_ = reverse sort \$ /. r , qw p ekca lre uJ reh
ts p , map \$ _. \$ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP3UqJGPeouIeTNHoEQI0UACfWiVqPsQjbTb0Npx4Ooh77WDPjWgAoKn7
wdc0/N6/KZqgMhpZBVDSVASO
=5BTn
-----END PGP SIGNATURE-----

Eric J. Roode, Sep 27, 2003