-
Notifications
You must be signed in to change notification settings - Fork 40
/
MissingNumber.js
30 lines (28 loc) · 918 Bytes
/
MissingNumber.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Source : https://leetcode.com/problems/missing-number
// Author : Dean Shi
// Date : 2017-09-06
/***************************************************************************************
*
* Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the
* one that is missing from the array.
*
* For example,
* Given nums = [0, 1, 3] return 2.
*
* Note:
* Your algorithm should run in linear runtime complexity. Could you implement it using
* only constant extra space complexity?
*
* Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating
* all test cases.
*
*
***************************************************************************************/
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function(nums) {
let sum = nums.length * (nums.length + 1) / 2
return nums.reduce((sum, num) => sum -= num, sum)
};