Find Missing Number

Find the missing number in a sequence from 1 to n using mathematical sum formula.

Code

Algorithms
const n = arr.length + 1;
const expectedSum = (n * (n + 1)) / 2;
const actualSum = arr.reduce((sum, x) => sum + x, 0);
return expectedSum - actualSum;

Parameters

Array with one missing number

Browser·fetch() may be limited by CORS

More JavaScript Snippets