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
    use v6; my @array = < 1 3 4 55 4 3 4 22 1 3 4 3 2 2 3 34 >; my @y = @array.classify({$_})\ .categorize({ .value.elems==1 || Nil }).[0].value».key; say "{@y.sort.join(',')} are the numbers that appear only once" if @y; #my @y = @array.classify({$_}).grep({ .value.elems == 1 })».key;