Set Difference

MediumData EngineeringArraySorting

Description

Given two arrays, return the distinct values that appear in the first array but not the second, preserving first-appearance order.

Examples

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

Values from the first array are kept only when they are absent from the second, with duplicates collapsed.

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

Values from the first array are kept only when they are absent from the second, with duplicates collapsed.

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

Values from the first array are kept only when they are absent from the second, with duplicates collapsed.

Constraints

  • 0 ≤ each length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.