
- 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 missing element in an array of numbers in JavaScript
We are required to write a JavaScript function that takes in an array of numbers of length, say n. The array contains all the integers from 0 to n (including both 0 and n), but just one integer is missing, it can be any number and the array is not sorted. The task of our function is to find the missing number and return it in linear time and constant space.
Since the array contains all the numbers from 0 to n but one, we can simply calculate the sum of all the elements of the array in linear time.
And then we can subtract it from the sum of first n natural numbers which can be calculated in constant time and space. The difference between the both will be our missing number.
Example
Following is the code −
const arr = [3, 7, 8, 10, 11, 0, 2, 6, 1, 4, 5]; const findMissing = (arr = []) => { const sum = arr.reduce((acc, val) => acc + val); const { length: num } = arr; const correctSum = (num * (num + 1)) / 2; return diff = correctSum - sum; return diff; }; console.log(findMissing(arr));
Output
Following is the console output −
9
- Related Articles
- Finding the nth missing number from an array JavaScript
- Finding the majority element of an array JavaScript
- Finding the first redundant element in an array - JavaScript
- Finding even length numbers from an array in JavaScript
- Find missing element in a sorted array of consecutive numbers in Python
- Find missing element in a sorted array of consecutive numbers in C++
- Finding all duplicate numbers in an array with multiple duplicates in JavaScript
- Finding sum of every nth element of array in JavaScript
- Finding the missing number in an arithmetic progression sequence in JavaScript
- Detecting the largest element in an array of Numbers (nested) in JavaScript
- Finding first unique element in sorted array in JavaScript
- Finding the rotation of an array in JavaScript
- Finding the mid of an array in JavaScript
- Finding degree of subarray in an array JavaScript
- Finding missing letter in a string - JavaScript

Advertisements