Skip to content

Instantly share code, notes, and snippets.

@jesalg
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 = ['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