Skip to content

Instantly share code, notes, and snippets.

@ayojimoh
Created October 3, 2019 20:26
Show Gist options
  • Save ayojimoh/7e825f33b174d9a32e8b9131b4060527 to your computer and use it in GitHub Desktop.
Save ayojimoh/7e825f33b174d9a32e8b9131b4060527 to your computer and use it in GitHub Desktop.
HackerRank Missing Numbers
// Complete the missingNumbers function below.
function missingNumbers(arr, brr) {
var arrCt = {};
var brrCt = {};
var missing = [];
var curr;
//10
//203 204 205 206 207 208 203 204 205 206
//13
//203 204 204 205 206 207 205 208 203 206 205 206 204
//create brr hash with counts
for (var i = 0; i < brr.length; i++) {
curr = brr[i]
if (brrCt[curr]) {
//if entry exists, increment value counter
brrCt[curr] += 1;
} else {
//if no entry exists, start counter at 1
brrCt[curr] = 1;
}
}
//create arr hash with counts
for (var j = 0; j < arr.length; j++) {
curr = arr[j]
if (arrCt[curr]) {
//if entry exists, increment value counter
arrCt[curr] += 1;
} else {
//if no entry exists, start counter at 1
arrCt[curr] = 1;
}
}
//iterate the brr hash
for (const [key, value] of Object.entries(brrCt)) {
//if doesn't exist in arr or count is different add to missing
if((key in arrCt) && (arrCt[key] == brrCt[key])) {
//skip
} else {
missing.push(key);
}
}
return missing;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment