Back to Problem
Official
2378 views
Editorial: Intersection of Two Arrays
by admin ·
26
1 Intuition
Sets remove duplicates and give O(1) membership checks.
2 Approach
Convert both arrays to sets, intersect them, and sort the result.
3 Complexity Analysis
Time O(n + m + k log k), Space O(n + m).
4 Solution Code
class Solution:
def intersection(self, nums1: list[int], nums2: list[int]) -> list[int]:
return sorted(set(nums1) & set(nums2))
5 Code Walkthrough
The reference solution below implements the approach step by step.