Find Common Elements Between Two Arrays

You are given two integer arrays nums1 and nums2 of sizes n and m, respectively. Calculate the following values:

  • answer1: the number of indices i such that nums1[i] exists in nums2.
  • answer2: the number of indices i such that nums2[i] exists in nums1.

Return [answer1, answer2].

Example 1
Inputnums1 = [2,3,2], nums2 = [1,2]
Output[2,1]
The value 2 appears at indices 0 and 2 in nums1, and the value 2 at index 1 in nums2 exists in nums1.
Example 2
Inputnums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
Output[3,4]
The elements at indices 1, 2, and 3 in nums1 exist in nums2, and the elements at indices 0, 1, 3, and 4 in nums2 exist in nums1.

Constraints

  • n == nums1.length
  • m == nums2.length
  • 1 <= n, m <= 100
  • 1 <= nums1[i], nums2[i] <= 100

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate