Find what is in array1 and not in array2

T

The Poor

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
 
K

Kiel R Stirling

John Bokma said:
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))


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
 
J

James E Keenan

The Poor said:
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
 
E

Eric J. Roode

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

(e-mail address removed).2y.net (The Poor) wrote in @posting.google.com:
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-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top