Set Intersection

MediumData EngineeringArray

Description

Given two arrays treated as sets, return the distinct values present in both, ordered by their first appearance in the first array.

Examples

Input:[1,2,3,4], [2,4,6]
Output:[2,4]
Explanation:

Only values found in both arrays are kept, ordered by where they first appear in the first array.

Input:[1,2,3], [4,5]
Output:[]
Explanation:

Only values found in both arrays are kept, ordered by where they first appear in the first array.

Input:[1,1,2], [2,2]
Output:[2]
Explanation:

Only values found in both arrays are kept, ordered by where they first appear in the first array.

Constraints

  • 0 ≤ each length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.