Skip to content

Instantly share code, notes, and snippets.

@jesalg
Last active August 29, 2015 13:58
Show Gist options
  • Save jesalg/10341948 to your computer and use it in GitHub Desktop.
Save jesalg/10341948 to your computer and use it in GitHub Desktop.
Simple function to remove duplicates
class Array
def remove_dups!
dup_count = Hash.new(0)
self.each do |val|
dup_count[val] += 1
end
dup_count.each do |val, count|
if count > 1
(count-1).times { self.delete_at self.rindex(val) }
end
end
end
end
emails = ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']
emails.remove_dups!
puts emails.to_s # ["[email protected]", "[email protected]", "[email protected]"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment