Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Compute the intersection of two arrays

Return the unique intersection elements of two arrays as a sorted list.

Python practice10 minHashing & SetsBeginnerLast updated March 26, 2026

Problem statement

Given two integer arrays nums1 and nums2, return a sorted list of unique integers that appear in both arrays. The result should contain no duplicates and should be sorted in ascending order. Aim for O(n + m) expected time using hashing/set operations, where n and m are the lengths of the two arrays.

Task

Use sets to deduplicate and compute intersection efficiently, then return the result in sorted order for deterministic output.

Examples

Example with duplicates

Input

nums1 = [4,9,5], nums2 = [9,4,9,8,4]

Output

[4, 9]

Common elements are 4 and 9; duplicates are removed and result is sorted.

Input format

Two lists of integers: intersection_sorted(nums1, nums2)

Output format

A list of unique integers that appear in both arrays, sorted ascending.

Constraints

0 <= len(nums1), len(nums2) <= 10^5; elements are integers within 32-bit signed range.

Samples

Sample 1

Input

intersection_sorted([1,2,2,1], [2,2])

Output

[2]

2 is the only common element; duplicates removed.