Find the missing number in a sequence from 1 to n using mathematical sum formula.
Code
Algorithmsconst 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
Array Difference
Find elements in the first array that are not present in the second array.
Array Frequencies
Count how many times each value appears in an array and return a frequency map.
Array Head
Get the first n elements of an array.
Array Intersection
Find common elements that exist in both arrays.
Array Tail
Get the last n elements of an array.
Array Union
Combine two arrays and remove duplicates to produce a union.