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 - Kelly #12

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
26 changes: 23 additions & 3 deletions lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
# 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 is the number of items of one array and m is the number of items in the other
# Space complexity: O(n)

Choose a reason for hiding this comment

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

👍

def intersection(array1, array2)
raise NotImplementedError
if array1 == nil || array2 == nil
return []
end
if array1.length < array2.length

Choose a reason for hiding this comment

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

It doesn't really matter which array you put into the hash table since you have to go through both. So this if statement is a bit wasted.

larger = array2
smaller = array1
else
larger = array1
smaller = array2
end
my_hash = Hash.new()
smaller.length.times do |num|
my_hash[smaller[num]] = 1
end
common_elements = []
larger.length.times do |num_1|
if my_hash.include? larger[num_1]
common_elements << larger[num_1]
end
end
return common_elements
end