Skip to content

Commit a5a9081

Browse files
committed
Adding Efficient Solution for Problem - 349 - Intersection Two Arrays
1 parent 27a4529 commit a5a9081

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 349
6+
## Problem Name: Intersection of Two Arrays
7+
##===================================
8+
#Given two arrays, write a function to compute their intersection.
9+
#
10+
#Example 1:
11+
#
12+
#Input: nums1 = [1,2,2,1], nums2 = [2,2]
13+
#Output: [2]
14+
#
15+
#Example 2:
16+
#
17+
#Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
18+
#Output: [9,4]
19+
class Solution:
20+
def intersection(self, nums1, nums2):
21+
set1 = set(nums1) #Type conversion list to set
22+
set2 = set(nums2) #Type conversion list to set
23+
#print(set1)
24+
#print(set2)
25+
if len(set1) < len(set2): #Condition-check: if set1's length is less than set2
26+
return self.s_intersection(set1, set2) #We return helper method starts from set1 to set2
27+
else: #Condition-check: Else
28+
return self.s_intersection(set2, set1) #We return helper method starts from set2 to set1
29+
def s_intersection(self, nums1, nums2): #Defining helper method takes two list as input
30+
return [i for i in set1 if i in set2] #Returns the list in which element i is in set1 and set2 or vice-verca.

0 commit comments

Comments
 (0)