- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Finding the nth missing number from an array JavaScript

Suppose, we have a strictly increasing sequence of number (increasing by a unit) in which some elements are missing like this −

const arr = [2,3,4,7,11];

We are required to write a JavaScript function that takes in one such array as the first argument and a single number, say n, as the second argument.

The function should find the nth element that missing from the array.

For example −

If for the above array, n = 4;

Then the output should be 8, because

The missing elements are −

1, 5, 6, 8

## Example

const arr = [2, 3, 4, 7, 11]; const findMissing = (arr = [], n) => { let el = 0; let diff = 0; for(let i=0; i<arr.length; ++i) { const difference = arr[i] - el - 1; const sum = diff + difference; if(sum>=n) { break; }; diff = sum; el = arr[i]; } return el + n - diff; }; console.log(findMissing(arr, 4));

## Output

This will produce the following output −

8

- Related Questions & Answers
- Finding the nth prime number in JavaScript
- Finding a number and its nth multiple in an array in JavaScript
- Finding the missing number in an arithmetic progression sequence in JavaScript
- Finding missing element in an array of numbers in JavaScript
- Finding the only out of sequence number from an array using JavaScript
- Finding the nth day from today - JavaScript (JS Date)
- Finding unlike number in an array - JavaScript
- JavaScript Finding the third maximum number in an array
- Finding the nth power of array element present at nth index using JavaScript
- Finding the nth palindrome number amongst whole numbers in JavaScript
- Finding the nth element of the lucas number sequence in JavaScript
- Finding confusing number within an array in JavaScript
- Finding the third maximum number within an array in JavaScript
- Finding the missing number between two arrays of literals in JavaScript
- Finding matching pair from an array in JavaScript

Advertisements