Missing Numbers

  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn missing_numbers(arr: &[i32], brr: &[i32]) -> Vec<i32> {
        //Time complexity: O(a+b)
        //Space complexity (ignoring input): O(a+b)
        let mut arr_hash = std::collections::HashMap::new();
        for value in arr {
            match arr_hash.get(value) {
                Some(f) => arr_hash.insert(value, f + 1),
                None => arr_hash.insert(value, 1),
            };
        }
        let mut brr_hash = std::collections::HashMap::new();
        for value in brr {
            match brr_hash.get(value) {
                Some(f) => brr_hash.insert(value, *f + 1),
                None => brr_hash.insert(value, 1),
            };
        }
    
        let mut missing_values = Vec::new();
        for (&key, frequency_b) in brr_hash.iter(){
            let frequency_a = arr_hash.get(key).unwrap_or(&0);
            if frequency_a <= frequency_b{
                missing_values.push(*key);
            }
        }
    
        missing_values.sort_unstable();
        missing_values
    }