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

Sockets - Pauline #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
24 changes: 21 additions & 3 deletions lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
# Returns a new array to that contains elements in the intersection of the two input arrays
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) to hash in, O(m) to look up, where n is length of array1 and m is length of array 2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would characterize this as O(n + m) where n is the length array1 and m is the length of array2, since you have to loop through each array once.

# Space complexity: O(n) to add to hash table, O(n) for results array
def intersection(array1, array2)
raise NotImplementedError
intersection = []
return intersection if !array1 || !array2
if array1.length < array2.length
smaller = array1
larger = array2
else
smaller = array2
larger = array1
end

my_hash = {}
smaller.each do |num|
my_hash[num] = 1
end

larger.each do |num|
intersection << num if my_hash.include?(num)
end
return intersection
end