Created
June 26, 2018 17:46
-
-
Save Gabelbombe/441d61dbca86003855fb46ef9fe862f3 to your computer and use it in GitHub Desktop.
Find missing numbers in an array of numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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