-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0268.js
More file actions
32 lines (28 loc) · 848 Bytes
/
0268.js
File metadata and controls
32 lines (28 loc) · 848 Bytes
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
31
32
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function(nums) {
// sum of array
const sum_reducer = (accumulator, currentValue) => accumulator + currentValue;
const array_sum = nums.reduce(sum_reducer)
// expected sum = 0.5 * (length * (length+1))
const length = nums.length
let consecutive_sum = 0.5 * (length * (length + 1))
return consecutive_sum - array_sum
};
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function(nums) {
// expected sum = 0.5 * (length * (length+1))
const length = nums.length
let consecutive_sum = 0.5 * (length * (length + 1))
// remove all numbers present in the array
for(let i=0; i<length; i++) {
consecutive_sum -= nums[i]
}
// only the missing number remains
return consecutive_sum
};