268. Missing Number
https://leetcode.com/problems/missing-number/
Python
Hash Map and remove
class Solution:
def missingNumber(self, nums: List[int]) -> int:
possibles = set(range(len(nums)+1))
for num in nums:
possibles.remove(num)
return possibles.pop()
Linear solution from total
class Solution:
def missingNumber(self, nums: List[int]) -> int:
n = len(nums)
total = (n*(n+1))//2
for num in nums:
total -= num
return total
Javascript
var missingNumber = function(nums) {
let sum = 0;
for (let i in nums) {
sum += nums[i];
}
let n = nums.length;
let sumN = Math.floor((n * (n + 1)) / 2);
return sumN - sum;
};