I'm working on an algorithm problem (on leetcode) which is asking the following:
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
For example,
Given nums = [0, 1, 3] return 2.
My current answer is:
var missingNumber = function(nums) {
return nums.filter(function(item, index, arr) {
return arr[index] - arr[index - 1] > 1;
}).shift() - 1;
};
However, leetcode is using these two test cases (among some others) which make no sense to me:
Input: [0]
Expected: 1
Input: [0, 1]
Expected: 2
EDIT: also...
Input: [1]
Expected: 0
From what I understand, the algorithm is asking to return a single number that is missing from an array, given there is a number that is actually missing in the first place. Am I missing something here or are the instructions for this algorithm very unclear?

0untilarr.length+1, andreturnon the first that you don't find in the array where you expected it.[1]to return0. I'll edit my description.