All problems

Intersection of Two Arrays

Easy · hash set

You are given two lists of integers, nums1 and nums2. Return a list of the values that appear in both lists.

Each value must appear only once in the answer, even if it shows up several times in the input lists. The answer must be sorted from the smallest value to the largest. If the lists have nothing in common, return an empty list.

Examples

Example 1

Input: nums1 = [1, 2, 2, 1], nums2 = [2, 2]

Output: [2]

Only 2 appears in both lists. It is listed once, although it appears several times.

Example 2

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

Output: [4, 9]

Both 4 and 9 appear in both lists. In the answer they are in ascending order.

Example 3

Input: nums1 = [1, 2], nums2 = [3, 4]

Output: []

Constraints