Skip to content

Instantly share code, notes, and snippets.

@meetrajesh
Created June 14, 2012 14:29
Show Gist options
  • Save meetrajesh/2930703 to your computer and use it in GitHub Desktop.
Save meetrajesh/2930703 to your computer and use it in GitHub Desktop.
Remove consecutive duplicates in an array in Ruby
def remove_consecutive_duplicates(xs)
[xs.first] + xs.each_cons(2).select do |x,y|
x != y
end.map(&:last)
end
remove_consecutive_duplicates([1, 2, 2, 3, 1])
#=> [1,2,3,1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment