in reply to Re: how to find combine common elements of an array?
in thread how to find combine common elements of an array?

Someone posted a related question recently, How can sets be recursively concatenated when intersecting with each other an so I've revisited my solution here. My primary concern with this particular algorithm is that it not duplicate effort. However, given more consideration I decided to go with a solution that was easier to understand in my new suggestion, Re: How can sets be recursively concatenated when intersecting with each other:

use strict; use warnings; my @array = map {[split]} ('11 12', '11 13', '9 8 7', '3 4', '11 4'); ORIG: for (my $i=0; $i<=$#array; $i++) { for my $j ($i+1..$#array) { my %seen; my @unique = grep {! $seen{$_}++} @{$array[$i]}, @{$array[$j]} +; if (@unique < @{$array[$i]} + @{$array[$j]}) { $array[$i] = \@unique; splice @array, $j, 1; redo ORIG; } } $array[$i] = join ' ', @{$array[$i]}; } print "$_\n" for (@array);

However, my favorite solution is jaredor's below: Re^2: how to find combine common elements of an array?