Skip to content

Instantly share code, notes, and snippets.

@sAVItar02
Created November 3, 2024 05:43
Show Gist options
  • Save sAVItar02/55c06023d0e4ce7544ea4fae0d09b305 to your computer and use it in GitHub Desktop.
Save sAVItar02/55c06023d0e4ce7544ea4fae0d09b305 to your computer and use it in GitHub Desktop.
Missing number
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function(nums) {
let sum = ((nums.length) * (nums.length + 1)) / 2;
let sum2 = 0;
for(let i=0; i<nums.length; i++) {
sum2 += nums[i];
}
return sum - sum2;
};
// Find the sum of all numbers in the range by the formula (n*(n+1)) / 2
// Find the sum of all numbers in the array
// Subtract both sums to get the answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment