Skip to main content
Back to problems
#1213
Easy Algorithms

Intersection of three sorted arrays

Array Hash Table Binary Search Counting
80.0% acceptance
Mar 31, 2026
616
26

No description available.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn arrays_intersection(arr1: Vec<i32>, arr2: Vec<i32>, arr3: Vec<i32>) -> Vec<i32> {
    let (mut i, mut j, mut k) = (0, 0, 0);
    let mut result = Vec::new();
    while i < arr1.len() && j < arr2.len() && k < arr3.len() {
      if arr1[i] == arr2[j] && arr2[j] == arr3[k] {
        result.push(arr1[i]);
        i += 1;
        j += 1;
        k += 1;
      } else {
        let mx = arr1[i].max(arr2[j]).max(arr3[k]);
        if arr1[i] < mx { i += 1; }
        if arr2[j] < mx { j += 1; }
        if arr3[k] < mx { k += 1; }
      }
    }
    result
  }
}