missingNumber()

Joseph K Abe
1 min readNov 13, 2021

This weeks coding challange is to find the missing number in a string from 1 to 100. I came to a very quick and simple solution to this one. If we know there is only one number missing in the array all we have to do is make sure the next number in the array is present. When using a for loop each individual number would be arr[i] and the proceeding number will be arr[i + 1]. If arr[i + 1] does not equal arr[i] + 1 that would mean our missing number is arr[i] + 1 !.

fuction missingNumber(arr){
for(let i = 0; i < arr.length; i++){
if (arr[i + 1] != arr[i] = 1){
return arr[i] + 1
}
}
}

Thats all there is to it! The problem would be a little more complicated if the array was not given to us in incrementing order, but all we would really have to do in that case would be to sort the array and use the same for loop in our sorted array.

It feels good to come up with such a simple solution every now and then. Who said coding had to be hard!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response