Leetcode #268 Missing Number

Leetcode #268 Missing Number

Difficulty: Easy

Sometimes it is good to use some easy questions to boost confidence.

The question basically is asking for missing number from a 0-based counting system.

  • I initialize an array with n+1 size, mark all values with '0'.
  • Iterate the new array against nums, if value exists, we can mark it as '1'
  • Iterate the new array and the missing number should be the index with '0' value.

Time complexity: O(2n)

Space complexity: O(n)

Link: Missing Number

var missingNumber = function(nums) {
    let mark =  Array(nums.length+1).fill(0);
    let result = -1;

    for(let i =0; i<nums.length;i++){
        mark[nums[i]]= 1;
    }

    for(let i =0; i<=mark.length;i++){
        if(mark[i] == 0){
            result = i;
        }
    }

    return result;
};