in reply to How to hash without the hash- basic
What kind of surreal contest is that?
Anyway, a rather quick solution would be to sort, and then look through the array, checking if the current element is identical to the previous or next. If no, it's unique.
Update: now with code:
# intentionally no warnings used. my @array = qw(1 3 4 55 4 3 4 22 1 3 4 3 2 2 3 34); @array = sort @array; my $prev; for (0..$#array) { my $i = $array[$_]; print "$i\n" if $i != $prev && $i != $array[$_+1]; $prev = $i; }
Second update: Another idea. This one is O(n^2) in theory, but the regex engine being fast makes up for that in practise for low-ish values of n:
my @array = qw(1 3 4 55 4 3 4 22 1 3 4 3 2 2 3 34); my $template = join ' ', @array; study $template; for (@array) { my @matches = ($template =~ /\b$_\b/g); print "$_\n" if 1 == @matches; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to hash without the hash- basic
by Util (Priest) on Oct 26, 2011 at 19:58 UTC | |
by moritz (Cardinal) on Oct 27, 2011 at 04:48 UTC |