#2540
Easy Algorithms Minimum common value
Array Hash Table Two Pointers Binary Search
58.0% acceptance
Feb 25, 2026
1233
42
Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return
the minimum integer common to both arrays. If there is no common integer amongst
nums1 and nums2, return -1.
Note that an integer is said to be common to nums1 and nums2 if both arrays have at
least one occurrence of that integer.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn get_common(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
let (mut i, mut j) = (0, 0);
while i < nums1.len() && j < nums2.len() {
match nums1[i].cmp(&nums2[j]) {
std::cmp::Ordering::Equal => return nums1[i],
std::cmp::Ordering::Less => i += 1,
std::cmp::Ordering::Greater => j += 1,
}
}
-1
}
}