Solving Missing Number Algorithm problem in JavaScript

Perezchristian
3 min readFeb 16, 2021

This week I’ve been practicing solving algorithm problems on LeetCode to better prepare myself for job assessment tests. In this post, I will go over one algorithm problem that I assessed and derived the solution for.

Problem: Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

Constraints:

  • n == nums.length
  • 1 <= n <= 104
  • 0 <= nums[i] <= n
  • All the numbers of nums are unique.

Function Given:

var missingNumber = function(nums) {
};

To start off, it’s my personal preference to change the function given to be typed in standard JavaScript ES6 convention. The result is written below:

const missingNumber = (nums) => {

};

Step #1: Since we are given the array nums, we know that we will be referring to the array in order to find out the range of the array. For this example, this array will represent our test code array [3,0,1]. Based on the Gauss’ formula, we will express the expected sum by defining it with the Sum of n which in this case will be the following:

let expectedSum = nums.length * (nums.length + 1)/2;

nums.length is calculating the length of the array, so for our test case the formula will be shown as: 3 * (3+1)/2

Using the order of PEMDAS we get 6. This equation represents what the sum should be of the missing number which is 2 plus the other numbers in the array which are 0, 1, and 3. Adding these up we get 2+0+1+3 = 6.

Step #2: We will define that actual sum to be if we were to start with no amounts in the array which will be 0. So we will define it as such below:

let actualSum = 0

Step #3: We will use the for loop iteration statement in order to iterate until a specified condition evaluates to false.

for ([initialExpression]; [conditionExpression]; [incrementExpression])
statement

We will show the above for loop statement description as:

for(let i = 0 ; i <= nums.length ; i++){

}

Which simply just says we will start at the index (i) equal to 0, when i is less then or equal to the array length, we will increment the amount.

Within the for loop statement curly braces we will need to set the conditional statement for when there is an amount within the array. To do so we will type:

if(nums[i])

After that, we define the equation for when we do have index amounts within the array to add to the actual sum:

actualSum += nums[i]

So if we fill in the amounts we get, 0+3+0+1 = 4.

Step #4: After the for loop statement, we need to return the last formula to read when you take the summation of what was expected (i.e 0+1+2+3) and subtracting what we actually got (i.e 3+0+1) we get:

return expectedSum — actualSum

This translates to 6–4 = 2. Two is the amount that is the missing number.

Summary: The below function is what we have as the solution to this problem.

const missingNumber = (nums) => {
let expectedSum = nums.length * (nums.length + 1)/2;
let actualSum = 0;

for(let i = 0 ; i <= nums.length ; i++){
if(nums[i])
actualSum += nums[i]
}
return expectedSum — actualSum;
};

I hope you understood one of the many solutions to this algorithm problem!

--

--

Perezchristian

Flatiron School Graduate with finance, budget, and tax experience.