From LeetCode 349. Intersection of Two Arrays
Description
Given two integer arrays nums1
and nums2
, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
Solution
We need to know one data structure : unordered set
.
Set & List comparison
Set
will use more space than list
, and compute speed is lower
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
return list(set(nums1) & set(nums2))