Can someone help me get started on this challenge:
Given an array of 99,999 unique numbers ranging from 1 to 100,00 in a random order, find the one number that is missing from the list.
I'm not sure how to start thinking about it.
Can someone help me get started on this challenge:
Given an array of 99,999 unique numbers ranging from 1 to 100,00 in a random order, find the one number that is missing from the list.
I'm not sure how to start thinking about it.
Except for the missing number, you are describing an arithmetic progression, which has a nifty formula to calculate its sum. So you could loop over the array, sum it, and then subtract that from the formula. The difference would be the missing element:
function missing(arr) {
var sum = 0;
for (var i = 0, len = arr.length; i < len; ++i) {
sum += arr[i];
}
var expected = 100000 * (1 + 100000) / 2;
var missing = expected - sum;
return missing;
}
If you want to do it without wasting any space storing numbers,
start with 1+2+3+4+... sum
Then subtract each number in the array from the sum