Skip to content
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
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: n*m or quadratic time complexity that is dependent on length of smaller array
# Space complexity: Up to On after sorting (in place is possible) where n is length of smaller array

Choose a reason for hiding this comment

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

I believe space complexity is O(1) for quadratic time complexity/insertion sort.
Source 1: Comparison Sort
Source 2: Time Complexity

def intersection(array1, array2)
raise NotImplementedError
unless array1 && array2
return []
end

if array1.length < array2.length
smaller = array1
larger = array2.sort
else
smaller = array2
larger = array1.sort
end

i = 0
result = []
smaller.each do |num|
larger.each do |other|
if num == other
result << num
end
end
end
return result

Choose a reason for hiding this comment

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

Nice work on this algorithm. Your naming and intent are both very clear. 🦄 ✨

end