Skip to content

Instantly share code, notes, and snippets.

@Gabelbombe
Created June 26, 2018 17:46
Show Gist options
  • Save Gabelbombe/441d61dbca86003855fb46ef9fe862f3 to your computer and use it in GitHub Desktop.
Save Gabelbombe/441d61dbca86003855fb46ef9fe862f3 to your computer and use it in GitHub Desktop.
Find missing numbers in an array of numbers
// Solution: sort numbers first, then test the offset(s)
// Disclosure: this is not a great idea for 4 billion numbers
// and is completely inefficient
var numArray = [ 2, 3, 1, 5, 7, 10 ];
var findMissingNumbers = function (numArray)
{
var missing = [];
numArray.sort(
function (a, b)
{
return (a - b);
}
);
for (var i = 0 ; i < numArray.length ; i++)
{
if (numArray[i + 1] - numArray[i] > 1)
{
missing.push (numArray[i + 1] - numArray[i]);
}
}
return missing;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment