Missing Numbers

Sort by

recency

|

1254 Discussions

|

  • + 0 comments

    JS solution using reduce. EZ

    function missingNumbers(arr, brr) {
        
        const groupA = arr.reduce((acc, n) => {
            acc[n] = (acc[n] || 0) + 1;
            return acc;
        }, {});
        
        const groupB = brr.reduce((acc, n) => {
            acc[n] = (acc[n] || 0) + 1;
            return acc;
        }, {});
        
        var missing = [];
        for(const nu of brr){
            if(groupA[nu] !== groupB[nu] && missing.indexOf(nu) < 0){
                missing.push(nu);
            }
        }
       
        return missing.sort(function(a, b) {
            return a - b;
        });
    }
    
  • + 0 comments

    Counting Sort is your friend, and the answer to many problems of this nature.

    O(m + n) solution is the goal.

  • + 0 comments

    Most simple solution that i could think of: def missingNumbers(arr, brr): for item in arr: if brr.contains(item): brr.remove(item) brr = sorted(set(brr),key = int) return brr

  • + 0 comments
    def missingNumbers(arr, brr):
        # Write your code here
        
        arr_freq = Counter(arr)
        brr_freq = Counter(brr)
        
        missing = []
        
        for key in brr_freq:
            if brr_freq[key] != arr_freq[key]:
                missing.append(key)
                
        return sorted(missing)
    
  • + 0 comments

    Time Complexity =

    def missingNumbers(arr:list, brr:list):
        sol = []
        
        arr_frequency = Frequency(arr)
        brr_frequency = Frequency(brr)
    
        for number , freq in brr_frequency.items():
            if number not in arr_frequency:
                sol.append(number)
            elif number in arr_frequency:
                if arr_frequency[number] != freq: 
                    sol.append(number)
        return sorted(sol)
    
    def Frequency(elements:list):
        temp = {}
        for element in elements:
            if element not in temp:
                temp[element] = 1
            else:
                temp[element] += 1
        return temp