Intersection of Two Arrays II
easyFind intersection of two arrays including duplicates
Intersection of Two Arrays II
Key Insight
Sort both arrays. Equal elements → add to result and advance both. Otherwise advance the smaller pointer.
Step 1Sort Both
nums2: [2, 2]
i
j
1
01
12
22
3→→same direction
Sort both arrays. nums1=[1,1,2,2], nums2=[2,2]. Use two pointers.
1 / 5