Sha256: 4f2cbf9172270a1d36a451ab8ad331d9dc50cb33eade7bc24f50b8f556fd97b3

Contents?: true

Size: 485 Bytes

Versions: 6

Compression:

Stored size: 485 Bytes

Contents

class Array

  # In Statistics mode is the value that occurs most frequently
  # in a given set of data. This method returns an array in case
  # their is a tie.
  #
  #   [:a, :b, :c, :b, :d].mode  #=> [:b]
  #   [:a, :b, :c, :b, :a].mode  #=> [:a, :b]
  #
  # Returns an Array of most common elements.
  #
  # CREDIT: Robert Klemme

  def mode
    max = 0
    c = Hash.new 0
    each {|x| cc = c[x] += 1; max = cc if cc > max}
    c.select {|k,v| v == max}.map {|k,v| k}
  end

end

Version data entries

6 entries across 5 versions & 1 rubygems

Version Path
facets-2.9.2 lib/core/facets/array/mode.rb
facets-2.9.2 src/core/facets/array/mode.rb
facets-2.9.1 lib/core/facets/array/mode.rb
facets-2.9.0 lib/core/facets/array/mode.rb
facets-2.9.0.pre.2 lib/core/facets/array/mode.rb
facets-2.9.0.pre.1 lib/core/facets/array/mode.rb