ExamAdda Logo

Intersection of Two Arrays

Easy

Given two integer arrays nums1 and nums2, return their intersection.

Each element in the result must be unique, even if it appears multiple times in both arrays.

Return the result in ascending order.

Example 1

Input

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

Output

[2]

Explanation

The element 2 appears in both arrays. Duplicate occurrences are included only once.

Example 2

Input

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

Output

[4, 9]

Explanation

The common elements are 4 and 9. The result is sorted in ascending order.

Constraints

1 <= nums1.length
nums2.length <= 1000
-1000 <= nums1[i]
nums2[i] <= 1000

Hints:

Hint 1

Store the elements of one array in a HashSet for efficient lookup.

Hint 2

Traverse the second array and collect elements that exist in the first array. Use another set to avoid duplicates, then sort the result.

Author & Technical Reviewer

Chatla Jithin Sri Surya

Written by: Chatla Jithin Sri Surya

<p>IIEST Shibpur | SDE | AI, ML &amp; Gen AI | DSA | Building Intelligent, Scalable Software</p>

Chatla Jithin Sri Surya

Technically reviewed by: Chatla Jithin Sri Surya

<p>IIEST Shibpur | SDE | AI, ML &amp; Gen AI | DSA | Building Intelligent, Scalable Software</p>

Published

Nov 14, 2026

Last updated

Nov 14, 2026

Last technically reviewed

Nov 14, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output