class Roadie::Deduplicator

Attributes

input[R]
latest_occurance[R]

Public Class Methods

apply(input) click to toggle source
# File lib/roadie/deduplicator.rb, line 3
def self.apply(input)
  new(input).apply
end
new(input) click to toggle source
# File lib/roadie/deduplicator.rb, line 7
def initialize(input)
  @input = input
  @duplicates = false
end

Public Instance Methods

apply() click to toggle source
# File lib/roadie/deduplicator.rb, line 12
def apply
  # Bail early for very small inputs
  input if input.size < 2

  calculate_latest_occurance

  # Another early bail in case we never even have a duplicate value
  if has_duplicates?
    strip_out_duplicates
  else
    input
  end
end

Private Instance Methods

calculate_latest_occurance() click to toggle source
# File lib/roadie/deduplicator.rb, line 33
def calculate_latest_occurance
  @latest_occurance = input.each_with_index.each_with_object({}) do |(value, index), map|
    @duplicates = true if map.has_key?(value)
    map[value] = index
  end
end
has_duplicates?() click to toggle source
# File lib/roadie/deduplicator.rb, line 29
def has_duplicates?
  @duplicates
end
strip_out_duplicates() click to toggle source
# File lib/roadie/deduplicator.rb, line 40
def strip_out_duplicates
  input.each_with_index.select { |value, index|
    latest_occurance[value] == index
  }.map(&:first)
end