From a90c1de5f3c24a3fc9f748cc46768b164bbd39ed Mon Sep 17 00:00:00 2001 From: Carla Bosco Date: Sun, 21 Apr 2019 11:43:04 -0700 Subject: [PATCH] Added method with time and space complexities --- lib/array_intersection.rb | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index 478b24e..dec3165 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,6 +1,19 @@ # Returns a new array to that contains elements in the intersection of the two input arrays -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(m*n), where m is size of first array and n is the size of second array +# Space complexity: O(m), where m is the size of the smallest array def intersection(array1, array2) - raise NotImplementedError + result = [] + + if array1 == nil || array2 == nil + result = [] + else + array1.each do |n| + array2.each do |m| + if n == m + result << n + end + end + end + end + return result end