Skip to content

Instantly share code, notes, and snippets.

@mfilej
Created March 7, 2010 23:22
Show Gist options
  • Save mfilej/324717 to your computer and use it in GitHub Desktop.
Save mfilej/324717 to your computer and use it in GitHub Desktop.
class Array
def bump_duplicates!
current = max.succ
each_with_index do |element, i|
if slice(0...i).include? element
self[i] = current
current = current.succ
end
end
end
def bump_duplicates
dup.bump_duplicates!
end
end
# a = [1,3,3,2,5]
# => [1, 3, 3, 2, 5]
# a.increment_duplicates
# => [1, 3, 6, 2, 5]
# a
# => [1, 3, 3, 2, 5]
# a.increment_duplicates!
# => [1, 3, 6, 2, 5]
# a
# => [1, 3, 6, 2, 5]
# %w(a b c c d e e f g h h h i j k).bump_duplicates
# => ["a", "b", "c", "l", "d", "e", "m", "f", "g", "h", "n", "o", "i", "j", "k"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment