Skip to main content
Back to problems
#2032
Easy Algorithms

Two out of three

Array Hash Table Bit Manipulation
77.4% acceptance
Feb 25, 2026
815
54
Given three integer arrays nums1, nums2, and nums3, return a distinct array containing all values present in at least two out of the three arrays.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn two_out_of_three(nums1: Vec<i32>, nums2: Vec<i32>, nums3: Vec<i32>) -> Vec<i32> {
    use std::collections::HashSet;
    let s1: HashSet<i32> = nums1.into_iter().collect();
    let s2: HashSet<i32> = nums2.into_iter().collect();
    let s3: HashSet<i32> = nums3.into_iter().collect();
    let mut result: HashSet<i32> = HashSet::new();
    for &x in &s1 { if s2.contains(&x) || s3.contains(&x) { result.insert(x); } }
    for &x in &s2 { if s1.contains(&x) || s3.contains(&x) { result.insert(x); } }
    result.into_iter().collect()
  }
}