Missing Numbers

  • + 1 comment

    I found using an object in JS was quite a straightforward approach IMO.

    EDIT: To elaborate, my reason for choosing to use an object for this solution is because I found it to be a reasonable choice to store the numbers as keys and then add 1 for each occurance for the fully intact array and subtract 1 for each occurance of the array that was incomplete. Thereafter, if any of the resulting values of the object keys are greater than 0, they are considered as missing. Cheers

    Here's my solution:

        let numCounts = {};
    
        for(let i = 0; i < brr.length; i++){
    
            numCounts[brr[i]] = (numCounts[brr[i]] === undefined) ? 1 :
            numCounts[brr[i]]+1;
    
            if(arr[i] !== undefined){
                numCounts[arr[i]] = (numCounts[arr[i]] === undefined) ? -1 :
                numCounts[arr[i]]-1;
            }
        }
        let k = Object.keys(numCounts);
        let results = [];
    
        for(let i = 0; i < k.length; i++){
            if(numCounts[k[i]] > 0) { results.push(k[i]); }
        }
        return results;