LeetCode Problem - Link

Problem

Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.

 

Example 1:

Input: nums = [4,3,2,7,8,2,3,1]
Output: [5,6]

Sequential Approach [No smarty business solution]

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var findDisappearedNumbers = function (nums) {
    let map = {}
    let max_num = 1
    for (var i = 0; i < nums.length; i++) {
        if (map[nums[i]]) {
            map[nums[i]] += 1
        } else {
            map[nums[i]] = 1
        }
        if (nums[i] > max_num) {
            max_num = nums[i]
        }
    }


    if (nums.length > max_num) {
        max_num = nums.length
    }


    let missing_arr = []
    for (var i = 1; i <= max_num; i++) {
        if (!map[i]) {
            missing_arr.push(i)
        }
    }


    return missing_arr
};