Problem · Array

Set Intersection

Learn this problem
EasyBlackrock logoBlackrockINTERNOA

Problem statement

You are given two integer arrays, first and second, each sorted in non-decreasing order.

Return their set intersection in ascending order. Every integer that appears in both arrays must appear exactly once in the result, even if it occurs multiple times in either input.

Function

setIntersection(first: int[], second: int[]) → int[]

Examples

Example 1

first = [1,2,3,4]second = [4,5,6]return = [4]

The only integer present in both arrays is 4.

Example 2

first = [-5,-1,0,3,7,10]second = [-8,-1,3,3,10,12]return = [-1,3,10]

The shared values are -1, 3, and 10. The repeated 3 in second contributes only one result value.

Example 3

first = [1,1,1,2,5,5]second = [1,1,3,5,5,5]return = [1,5]

Both arrays contain 1 and 5. Each shared value appears once in the set intersection.

Constraints

  • 1 ≤ first.length, second.length ≤ 10^5
  • -10^9 ≤ first[i], second[i] ≤ 10^9
  • first and second are sorted in non-decreasing order.
  • The returned array must contain no duplicate values.

More Blackrock problems

drafts saved locally
public int[] setIntersection(int[] first, int[] second) {
  // Write your code here
}
first[1,2,3,4]
second[4,5,6]
expected[4]
checking account