
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- Finding a number and its nth multiple in an array in JavaScript
- Finding the nth prime number 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 unlike number in an array - JavaScript
- Finding the nth power of array element present at nth index using JavaScript
- Finding the nth day from today - JavaScript (JS Date)
- JavaScript Finding the third maximum number in an array
- Find the Smallest Positive Number Missing From an Unsorted Array
- Finding the nth palindrome number amongst whole numbers in JavaScript
- Finding confusing number within an array in JavaScript
- Finding the nth element of the lucas number sequence in JavaScript
- Finding the third maximum number within an array in JavaScript
- Finding matching pair from an array in JavaScript

Advertisements