Skip to content

Instantly share code, notes, and snippets.

@ihower

ihower/hash.rb Secret

Last active June 5, 2017 08:28
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 ihower/48cdb4dc3725b52c7b1616bb60cd49d0 to your computer and use it in GitHub Desktop.
Save ihower/48cdb4dc3725b52c7b1616bb60cd49d0 to your computer and use it in GitHub Desktop.
arr1 = [1,2,3] # []
arr2 = [1,1,2] # [1]
arr3 = [1,1,2,2] # [1,2]
def find_dup_v2(arr)
unique_hash = {}
dup_arr = []
arr.each do |a| # O(N)
if unique_hash[a] # O(1)
dup_arr << a
else
unique_hash[a] = true
end
end
return dup_arr
end
puts find_dup_v2(arr1).to_s
puts find_dup_v2(arr2).to_s
puts find_dup_v2(arr3).to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment