Last active
August 3, 2020 11:45
-
-
Save lienista/9cdf83dbc5b6f2f7464d6e90324a7da3 to your computer and use it in GitHub Desktop.
(Algorithms in Javascript) Leetcode 268. Missing Number - Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const missingNumber = (nums) => { | |
| let len = nums.length; | |
| if(len===0) return 0; | |
| result = 0; | |
| for (let i=0; i<len; i++) { | |
| result += nums[i] - i; | |
| } | |
| return len - result; | |
| }; | |
| console.log(missingNumber([3,0,1])); | |
| console.log(missingNumber([9,6,4,2,3,5,7,0,1])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment