[ruby-it] contare frequenza su array
Antonio Cangiano
acangiano a gmail.com
Mer 4 Feb 2009 14:29:21 CET
On Wed, Feb 4, 2009 at 8:19 AM, Giampiero Zanchi <cidza a tin.it> wrote:
> che esista una strada migliore di quella intrapresa; ad esempio, come si
> fa a ottenere la frequenza degli elementi di un array? personalmente ho
> prodotto il seguente codice:
> a = [1,2,3,4,1,2,3,1,2,1,5,5]
Io farei cosė:
a.inject(Hash.new(0)) { |hash, item| hash[item] += 1; hash }
Ciao,
Antonio
--
http://antoniocangiano.com - Zen and the Art of Programming
http://math-blog.com - Mathematics is wonderful!
http://stacktrace.it - Aperiodico di resistenza informatica
Currently writing "Ruby on Rails for Microsoft Developers" for Wrox.
More information about the Ml
mailing list