4.8. Computing Union, Intersection, or Difference of Unique ListsProblemYou 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 ). SolutionThe 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 intersectionforeach $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 versionforeach $e (@a, @b) { $union{$e}++ && $isect{$e}++ } @union = keys %union; @isect = keys %isect; Union, intersection, and symmetric differenceforeach $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; } DiscussionThe 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) 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
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 See AlsoThe "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 |
|