#2540

Minimum Common Value

pupil · 390 · lc easy +24 · verified · 58% accepted · 1,234 likes · top 54%

Description

Given two sorted non-decreasing integer arrays nums1 and nums2, find and return the smallest value that appears in both arrays. Return -1 if no such common value exists.

Example 1:

Input: nums1 = [1,2,3], nums2 = [2,4]
Output: 2
Explanation: The smallest element common to both arrays is 2, so we return 2.

Example 2:

Input: nums1 = [1,2,3,6], nums2 = [2,3,4,5]
Output: 2
Explanation: There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned.

Code

1
2
3