-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProblem4_Intersection of Two Arrays_349.py
More file actions
24 lines (20 loc) · 1.14 KB
/
Problem4_Intersection of Two Arrays_349.py
File metadata and controls
24 lines (20 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# UCLA ACM ICPC: Interview Track Leetcode Problem Solutions
# Disclaimer: This is not the only valid solution and we do not claim our solutions
# to be optimal in terms of runtime or memory usage, we are merely giving example
# solutions to questions for learning purposes only
# Intersection of Two Arrays
# Leetcode Problem 349
# https://leetcode.com/problems/intersection-of-two-arrays/
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
outputlist = [] #this is the return list that we are returning
hashmap = {} #hash map for keeping track of nums1
for i,val in enumerate(nums1): #add everyvalue into hashmap, non duplicates
# because we dont care about the indices.
if val not in hashmap:
hashmap[val] = i
for i,val in enumerate(nums2): #check values in nums2
if val in hashmap and val not in outputlist: #if the value is in hashmap already
#and our value is not already in output, then append
outputlist.append(val)
return outputlist #return outputlist