Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ports - Amy M #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
# Returns a new array to that contains elements in the intersection of the two input arrays
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n+m) where n represents the length the smaller array, and m the larger.
# Space complexity: O(n), linear, because the additional space required is directly related to
# length "n" of the smaller array.
def intersection(array1, array2)
raise NotImplementedError
if array1 == nil || array2 == nil
return []
end
if array1.length < array2.length
amyesh marked this conversation as resolved.
Show resolved Hide resolved
larger = array2
smaller = array1
else
larger = array1
smaller = array2
end
my_hash = Hash.new()
smaller.each do |num|
my_hash[num] = 1
end
common_elements = []
larger.each do |num_1|
if my_hash.include? num_1
common_elements << num_1
end
end
return common_elements
end