LeetCode 1213. Intersection of Three Sorted Arrays

Problem Statement


use std::cmp;

impl Solution {
pub fn arrays_intersection(arr1: Vec<i32>, arr2: Vec<i32>, arr3: Vec<i32>) -> Vec<i32> {
let mut ans = vec![];
let (mut i, mut j, mut k) = (0, 0, 0);

while i < arr1.len() && j < arr2.len() && k < arr3.len() {
if arr1[i] == arr2[j] && arr2[j] == arr3[k] {
ans.push(arr1[i]);
}
let min_val = cmp::min(arr1[i], cmp::min(arr2[j], arr3[k]));
if arr1[i] == min_val {
i += 1;
}
if arr2[j] == min_val {
j += 1;
}
if arr3[k] == min_val {
k += 1;
}
}

return ans;
}
}