in reply to Difference arrays.
Since you're concerned about memory, you could do something like a Merge Sort.
Memory: O(1) (Not counting @a, @b and @c)
Speed: O(A+B) (Assuming @a and @b already sorted. As good as the other solutions)
my @a = sort { $a <=> $b } (43,43,44); my @b = sort { $a <=> $b } (43,43); my @c; while (@a && @b) { if ($a[0] < $b[0]) { push @c, shift @a; } elsif ($a[0] > $b[0]) { die "Bad data"; } else { shift @a; shift @b; } } push @c, $_ for @a; die "Bad data" if @b;
A trivial change makes it non-destructive.
Update: Fixed bug mentioned in replies. Tested.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Difference arrays.
by GrandFather (Saint) on Sep 04, 2008 at 21:39 UTC | |
|
Re^2: Difference arrays.
by kyle (Abbot) on Sep 04, 2008 at 20:23 UTC |