Advertisement
aero2146

Intersection of Two Arrays II

Apr 10th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | None | 0 0
  1. class Solution:
  2.     def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
  3.         num_freq = dict()
  4.         res = []
  5.        
  6.         for num in nums1:
  7.             if num in num_freq:
  8.                 num_freq[num] += 1
  9.             else:
  10.                 num_freq[num] = 1
  11.        
  12.         for num in nums2:
  13.             if num in num_freq and num_freq[num] != 0:
  14.                 num_freq[num] -= 1
  15.                 res.append(num)
  16.        
  17.         return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement