349. Intersection of Two Arrays¶
Edited by Charles
Easy · Python · HashTable
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Create two sets of distinctive numbers in nums1 and nums2 respectively. Initialize the result list.
Original study notes follow, including intermediate snippets and earlier attempts.
- Create two sets of distinctive numbers in nums1 and nums2 respectively.
- Initialize the result list.
- Iterate one array and check if the current element is in the set created by nums2, meanwhile check if the element is in the result list (no duplicate).
- Since the result is initialized in the form of a list, we need to check duplicate values. If we initialize the result in the form of a set, we do not need to worry about duplication.
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | January 5, 2026 |
Python solution¶
# List
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
set_1 = set(nums1)
set_2 = set(nums2)
res = []
for num in nums1:
if num in nums2 and num not in res:
res.append(num)
return res
# Set
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
set_1 = set(nums1)
set_2 = set(nums2)
res = set()
for num in nums1:
if num in nums2:
res.add(num)
return list(res)