Sha256: c1b8231730c13913e2196ca92ed6b42e4a97d07b0a59e0ecbbf462439a4d0697
Contents?: true
Size: 989 Bytes
Versions: 5
Compression:
Stored size: 989 Bytes
Contents
# frozen_string_literal: true module Roadie class Deduplicator def self.apply(input) new(input).apply end def initialize(input) @input = input @duplicates = false end 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 attr_reader :input, :latest_occurance def has_duplicates? @duplicates end 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 def strip_out_duplicates input.each_with_index.select { |value, index| latest_occurance[value] == index }.map(&:first) end end end
Version data entries
5 entries across 5 versions & 1 rubygems