Skip to main content

Union Distinct

MediumData EngineeringArraySorting

Description

Given two arrays, return their union with duplicates removed, preserving the order of first appearance across the first array followed by the second.

Examples

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

Values from both arrays are combined and each distinct one is kept the first time it appears.

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

Values from both arrays are combined and each distinct one is kept the first time it appears.

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

Values from both arrays are combined and each distinct one is kept the first time it appears.

Constraints

  • 0 ≤ each length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.