-
-
Save ihower/48cdb4dc3725b52c7b1616bb60cd49d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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