/350. Intersection of Two Arrays II

350. Intersection of Two Arrays II

Easy
Arrays59.7% acceptance

Given two integer lists list_a and list_b, return a list containing their intersection. Each element in the result must appear as many times as it appears in both lists. The order of elements in the output list does not matter.

Example 1

Input: list_a = [3,3,2,1], list_b = [3,1,3]

Output: [3,3,1]

Explanation: Both lists have two 3's and one 1. Result can be [3,3,1] or [3,1,3].

Example 2

Input: list_a = [5,6,7], list_b = [7,8,5,5]

Output: [5,7]

Explanation: Intersection is 5 and 7. Only one 5 appears in both.

Constraints

  • 1 <= len(list_a) <= 1000
  • 1 <= len(list_b) <= 1000
  • 0 <= list_a[i] <= 1000
  • 0 <= list_b[i] <= 1000
Python (current runtime)

Case 1

Input: list_a = [10,20,10,30], list_b = [10,10,40]

Expected: [10,10]

Case 2

Input: list_a = [0,1,2,3], list_b = [3,2,1,0]

Expected: [0,1,2,3]

Case 3

Input: list_a = [1000], list_b = [1000,1000]

Expected: [1000]

Case 4

Input: list_a = [4,4,4,5], list_b = [4,5,5]

Expected: [4,5]