Find Missing Number

Woodelin florveus
2 min readNov 6, 2021

One problem I tried avoiding was the missing number problem. I did not have confidence in myself to find the actual missing integer in the array. However with a bit of research and resources available to me online I was able to stomach this problem. This problem is number 268 “Missing Number” posted in Leetcode. It states “Given array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.” Here is a closer look at the problem.

const missingNumber = nums => {

}
console.log(missingNumber([3,0,1]))

Here are a few examples and explanations below for a better perspective.

Input: nums = [3,0,1]
Output: 2
Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.

Deeper Dive Into the Problem.

I created a variable and set it to 0. The purpose of this is to use this variable to increment the numbers in the array and get us one step closer to the problem. Here’s a step-by-step below.

const missingNumber = nums => {let missing = 0

}
console.log(missingNumber([3,0,1]))

I began to start looping through the entire array. Going through each number. Once that has been established I decided to increment the variable created earlier to the current number. Furthermore, since we are going from the range I decided to subtract the index from the number to filter out that particular missing number. Here’s a closer look below.

const missingNumber = nums => {let missing = 0

for(let i = 0; i < nums.length; i++){
missing+=nums[i] - i
}
}
console.log(missingNumber([3,0,1]))

Once completed I subtracted the missing number from the length.

const missingNumber = nums => {let missing = 0

for(let i = 0; i < nums.length; i++){
missing+=nums[i] - i
}

return nums.length - missing

}
console.log(missingNumber([3,0,1]))

Thanks for reading. Any questions leave a comment.

--

--