Skip to content

Instantly share code, notes, and snippets.

@ihower

ihower/array.rb Secret

Last active December 10, 2017 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ihower/b7e8c3f658b49bd9e261876202810094 to your computer and use it in GitHub Desktop.
Save ihower/b7e8c3f658b49bd9e261876202810094 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_v1(arr)
unique_arr = []
dup_arr = []
arr.each do |a| # O(N)
if unique_arr.include?(a) # O(N)
dup_arr << a
else
unique_arr << a
end
end
return dup_arr
end
puts find_dup_v1(arr1).to_s
puts find_dup_v1(arr2).to_s
puts find_dup_v1(arr3).to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment