Find what is in array1 and not in array2

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

  1. The Poor

    The Poor Guest

    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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. "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
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    .2y.net (The Poor) wrote in news:30bcc0c9.0309260752.1d1e44b9
    @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-----
     
    Eric J. Roode, Sep 27, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Schramm

    array1 + array2 newb question

    Mike Schramm, Oct 13, 2005, in forum: Ruby
    Replies:
    9
    Views:
    143
    William James
    Oct 13, 2005
  2. Wybo Dekker
    Replies:
    1
    Views:
    395
    Yukihiro Matsumoto
    Nov 15, 2005
  3. Hal Vaughan
    Replies:
    8
    Views:
    120
    Thomas 'PointedEars' Lahn
    Feb 17, 2004
  4. Replies:
    7
    Views:
    132
    Marek Stepanek
    Jul 31, 2006
  5. Sunouchi

    Loading array1.js from xx.js file

    Sunouchi, Jan 20, 2010, in forum: Javascript
    Replies:
    3
    Views:
    112
    Sunouchi@
    Jan 22, 2010
Loading...

Share This Page