Sha256: ebce38db15461d3163095827d63119ef72f78dbcf35f325ef74e9a972eb0e414
Contents?: true
Size: 480 Bytes
Versions: 10
Compression:
Stored size: 480 Bytes
Contents
module Enumerable # Generates a hash mapping each unique symbol in the array # to the absolute frequency it appears. # # [:a,:a,:b,:c,:c,:c].frequency #=> {:a=>2,:b=>1,:c=>3} # # CREDIT: Brian Schröder # #-- # NOTE: So why not use #inject here? e.g. ... # # inject(Hash.new(0)){|p,v| p[v]+=1; p} # # Because it is a fair bit slower than the traditional definition. #++ def frequency p = Hash.new(0); each{ |v| p[v] += 1 }; p end end
Version data entries
10 entries across 9 versions & 2 rubygems