Skip to content

Instantly share code, notes, and snippets.

@jesalg jesalg/remove_dups.rb
Last active Aug 29, 2015

Embed
What would you like to do?
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 = ['jane@example.com', 'jon@example.com', 'jane@example.com', 'ron@example.com', 'ron@example.com', 'jon@example.com']
emails.remove_dups!
puts emails.to_s # ["jane@example.com", "jon@example.com", "ron@example.com"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.