Created
June 14, 2012 14:29
-
-
Save meetrajesh/2930703 to your computer and use it in GitHub Desktop.
Remove consecutive duplicates in an array in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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