From 2c11993806af4b8d91228274faf2576c325dc283 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 16 Apr 2021 14:12:30 -0700 Subject: [PATCH] Add brute force solution --- data_structures/arrays/intersection.rb | 29 ++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/data_structures/arrays/intersection.rb b/data_structures/arrays/intersection.rb index bd826a0..9633d7b 100644 --- a/data_structures/arrays/intersection.rb +++ b/data_structures/arrays/intersection.rb @@ -7,17 +7,38 @@ # @return {Integer[]} # -# Approach 1: +# Approach 1: Brute Force # -# Time Complexity: +# Time Complexity: O(n^2) # def intersect(arr1, arr2) + result = [] + + if arr1.length < arr2.length + shorter = arr1 + longer = arr2 + else + shorter = arr2 + longer = arr1 + end + + shorter.each do |matcher| + longer.each do |number| + next if number != matcher + result.push(number) + break + end + end + + result end + nums1 = [1, 2, 2, 1] nums2 = [2, 2] -intersect(nums1, nums2) +puts intersect(nums1, nums2) # => [2,2] + nums1 = [4, 9, 5] nums2 = [9, 4, 9, 8, 4] -intersect(nums1, nums2) +puts intersect(nums1, nums2) # => [4,9] \ No newline at end of file