Missing Numbers

Sort by

recency

|

109 Discussions

|

  • + 0 comments

    Python Solution:

    def missingNumbers(arr, brr):
        brr.sort()
        arr.sort()
        biggest_val = brr[-1]
        arr_count_arr = [0] * (biggest_val + 1)
        brr_count_arr = [0]* (biggest_val + 1)
        
        result = []
        # print(brr_count_arr)
        
        for i in range(len(brr)):
            brr_count_arr[brr[i]] += 1
        
        for i in range(len(arr)):
            arr_count_arr[arr[i]] += 1
        
        for i in range(len(brr_count_arr)):
            if brr_count_arr[i] != arr_count_arr[i]:
                result.append(i)
        
        return result
    
  • + 0 comments

    Java Solution

        public static List<Integer> missingNumbers(List<Integer> arr, List<Integer> brr) {
            List<Integer> arrCopy = new ArrayList<>(arr);
            Set<Integer> missing = new TreeSet<>();
            for (Integer b: brr) {
                if (!arrCopy.contains(b)) {
                    missing.add(b);
                } else { arrCopy.remove(b); }
            }
            return missing.stream().collect(Collectors.toList());
        }
    
  • + 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
    }
    
  • + 0 comments

    Python 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

    def missing_numbers(arr, brr):
        # Time complexity: O(a + b)
        # Space complexity (ignoring input): O(a + b)
        arr_dict = {}
        for value in arr:
            if value in arr_dict:
                arr_dict[value] += 1
            else:
                arr_dict[value] = 1
    
        brr_dict = {}
        for value in brr:
            if value in brr_dict:
                brr_dict[value] += 1
            else:
                brr_dict[value] = 1
    
        missing_values = []
        for key in brr_dict.keys():
            if key in arr_dict:
                if arr_dict[key] <= brr_dict[key]:
                    missing_values.append(key)
            else:
                missing_values.append(key)
    
        missing_values.sort()
        return missing_values
    
  • + 0 comments

    Python

    def missingNumbers(arr, brr):
        arr.sort()
        brr.sort()
    
        ai, bi = 0, 0
        res = []
        while ai < len(arr) and bi < len(brr):
            if arr[ai] > brr[bi]:
                if brr[bi] not in res:
                    res.append(brr[bi])
                bi += 1
            elif arr[ai] < brr[bi]:
                ai += 1
            else:
                ai += 1
                bi += 1
    
        res += sorted(set(brr[bi:]))
        return res