Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How to hash without the hash- basic

by moritz (Cardinal)
on Oct 26, 2011 at 13:25 UTC ( [id://933900]=note: print w/replies, xml ) Need Help??


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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://933900]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2024-04-25 04:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found