Skip to content

Instantly share code, notes, and snippets.

@abdellani
Created October 16, 2019 15:08
Show Gist options
  • Save abdellani/10078693bd552c1191e48ce8a7335440 to your computer and use it in GitHub Desktop.
Save abdellani/10078693bd552c1191e48ce8a7335440 to your computer and use it in GitHub Desktop.
require 'set'
def find_duplicates(array)
# write your code here
result=[]
set=Set[]
array.each do |element|
if !set.include?(element)
set.add(element)
else
result<< element
end
end
result
end
p find_duplicates([1, 2, 3, 1, 5, 6, 7, 8, 5, 2])
# => [1, 5, 2]
p find_duplicates([3, 501, 17, 23, -43, 67, 5, 888, -402, 235, 77, 99, 311, 1, -43])
# => [-43]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment