class StatisticalMethods::Leader

Finding a leader in the collection

Public Class Methods

new(array) click to toggle source
# File lib/statistical_methods/leader.rb, line 4
def initialize(array)
  @array = array
  @candidate = array.first
  @counter = 0
end

Public Instance Methods

find() click to toggle source
# File lib/statistical_methods/leader.rb, line 10
def find
  @array.each { |value| compare(value) }
  @candidate if @array.count(@candidate) > @array.size / 2
end

Private Instance Methods

compare(value) click to toggle source
# File lib/statistical_methods/leader.rb, line 17
def compare(value)
  @candidate == value ? increment : decrement(value)
end
decrement(value) click to toggle source
# File lib/statistical_methods/leader.rb, line 25
def decrement(value)
  @counter -= 1
  return if @counter == 0
  @candidate = value
  @counter = 1
end
increment() click to toggle source
# File lib/statistical_methods/leader.rb, line 21
def increment
  @counter += 1
end