Ruby Snippet: Eliminate consecutive duplicates

Programming in Ruby is really pleasant.

# P08 (**) Eliminate consecutive duplicates of list elements.

module ProblemEight
  COLLECTION = [:a, :a, :a, :a, :b, :c, :c, :a, :a, :d, :e, :e]

  def self.compress col
    col.inject [] {|arr, el| arr << el unless el == arr.last; arr}
  end

  def self.run
    puts compress(COLLECTION).to_s
  end
end

ProblemEight.run
This entry was posted in News. Bookmark the permalink.

Comments are closed.