FastPrepIntersection of Two Lists
Problem · Array

Intersection of Two Lists

Learn this problem
EasyLinkedIn logoLinkedInINTERNPHONE SCREEN

Problem statement

Given two integer lists listA and listB, return their set intersection: every distinct integer that occurs in both inputs.

Each common value must appear exactly once in the result. Return the values in increasing order so that the output is deterministic.

Function

intersectLists(listA: int[], listB: int[]) → int[]

Examples

Example 1

listA = [1,2,2,1]listB = [2,2]return = [2]

The only distinct value present in both lists is 2.

Example 2

listA = [4,9,5]listB = [9,4,9,8,4]return = [4,9]

4 and 9 occur in both inputs and are returned once each in increasing order.

Example 3

listA = []listB = [1,2,3]return = []

An empty list has no values in common with the other list.

Constraints

  • 0 <= listA.length, listB.length <= 200000
  • -10^9 <= listA[i], listB[i] <= 10^9
  • The returned values must be unique and sorted in increasing order.

More LinkedIn problems

drafts saved locally
public int[] intersectLists(int[] listA, int[] listB) {
  // Write your code here.
}
listA[1,2,2,1]
listB[2,2]
expected[2]
checking account