Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: How to find number of unique elemenst in array

by ysth (Canon)
on Nov 27, 2008 at 20:27 UTC ( [id://726477]=note: print w/replies, xml ) Need Help??


in reply to How to find number of unique elemenst in array

use strict; use warnings; use List::Util "reduce"; use Lingua::EN::Inflect "inflect"; use Algorithm::Loops "MapCar"; my @array = qw/A B C B D B D/; print map { map ( inflect("There PL(is,$_) $_ PL(element,$_) of "), $_->[1] +), "$_->[0]\n" } MapCar { [@_] } map { [sort keys %$_], [@$_{sort keys %$_}] } reduce { $a->{$b||$b}++; $a } {}, @array ;
Sometimes it amuses me to place arbitrary constraints on myself in answering a question. In this case, I decided I was limited to one statement and no (declared) variables other than the input.

Update: replaced an ugly reduce with MapCar.
Update: additional topicalization

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-26 00:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found