Find Missing Number

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.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Don’t Mix Controlled and Uncontrolled Values In React

How to create pagination with a contentlayer in next.js?

How to create pagination with a contentlayer in next.js?

Mimicking the Medium Clap Animation with Sketch and Haiku

Building a Desktop App in Go using Wails

JavaScript Async and Await — Your Asynchronous Buddies

VUESTUDIO: BEHIND THE SCENES

Bootstrap is bad?

Error: No router instance found.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Woodelin florveus

Woodelin florveus

More from Medium

Advantages of Pair Programming

Jean Bartik and Betty Snyder

How To Get An Edge Over The Competition Using Your Resume

Travelling Salesman Problem Using Simulated Annealing

Development on a weak system…