Book HomePerl CookbookSearch this book

4.8. Computing Union, Intersection, or Difference of Unique Lists

Problem

You have a pair of lists, each having unduplicated items. You'd like to find out which items are in both lists (intersection), one but not the other (difference), or either (union).

Solution

The following solutions need the listed initializations:

@a = (1, 3, 5, 6, 7, 8);
@b = (2, 3, 5, 7, 9);

@union = @isect = @diff = ();
%union = %isect = ();
%count = ();

Simple solution for union and intersection

foreach $e (@a) { $union{$e} = 1 }

foreach $e (@b) {
    if ( $union{$e} ) { $isect{$e} = 1 }
    $union{$e} = 1;
}
@union = keys %union;
@isect = keys %isect;

More idiomatic version

foreach $e (@a, @b) { $union{$e}++ && $isect{$e}++ }

@union = keys %union;
@isect = keys %isect;

Union, intersection, and symmetric difference

foreach $e (@a, @b) { $count{$e}++ }

foreach $e (keys %count) {
    push(@union, $e);
    if ($count{$e} == 2) {
        push @isect, $e;
    } else {
        push @diff, $e;
    }
}

Indirect solution

@isect = @diff = @union = ();

foreach $e (@a, @b) { $count{$e}++ }

foreach $e (keys %count) {
    push(@union, $e);
    push @{ $count{$e} == 2 ? \@isect : \@diff }, $e;
}

Discussion

The first solution most directly computes the union and intersection of two lists, neither containing duplicates. Two different hashes are used to record whether a particular item goes in the union or the intersection. We first put every element of the first array in the union hash, giving it a true value. Then processing each element of the second array, we check whether that element is already present in the union. If it is, then we put it in the intersection as well. In any event, it is put into the union. When we're done, we extract the keys of both the union and intersection hashes. The values aren't needed.

The second solution ("More idiomatic version") is essentially the same but relies on familiarity with the Perl (and awk, C, C++, and Java) ++ and && operators. By placing the ++ after the variable, we first look at its old value before incrementing it. The first time through it won't be in the union, which makes the first part of the && false, and the second part is consequently ignored. The second time that we encounter the same element, it's already present in the union, so we put it in the intersection as well.

The third solution uses just one hash to track how many times each element has been seen. Once both arrays have their elements recorded in the hash, we process those hash keys one at a time. If it's there, it goes in the union array. Keys whose values are 2 were in both arrays, so they are put in the intersection array. Keys whose values are 1 were in just one of the two arrays, so they are put in the difference array. The elements of the output arrays are not in the same order as the elements in the input arrays.

The last solution, like the previous one, uses just one hash to count how many times each element has been encountered. However, this time we choose the array within the @{ .... } block.

We compute the symmetric difference here, not the simple difference. These are set theoretic terms. A symmetric difference is the set of all the elements that are members of either @A or @B, but not of both. A simple difference is the set of members of @A but not of @B, which we calculated in Recipe 4.7.

See Also

The "Hashes (Associative Arrays)" section of Chapter 2 of Programming Perl; Chapter 5; we use hashes in a similar fashion in Recipe 4.6 and Recipe 4.7


Previous: 4.7. Finding Elements in One Array but Not AnotherPerl CookbookNext: 4.9. Appending One Array to Another
4.7. Finding Elements in One Array but Not AnotherBook Index4.9. Appending One Array to Another

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.