Question:

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

给定1≤a[i]≤n(n=数组的大小)的整数组,一些元素出现两次,其他元素出现一次。

查找所有未出现在数组中的[1,n]包含的元素。

你能在没有额外空间和O(n)运行时做吗?您可以假设返回的列表不被视为额外的空间

Example:

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

Solution:

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var findDisappearedNumbers = function(nums) {
    let diffArr=[];
    for (let i=1; i<nums.length+1; i++) {
        diffArr.push(i);
    }
    nums.forEach(item => {
        let s = diffArr.indexOf(item);
        if (s>-1) {
            diffArr.splice(s, 1);
        }
    })
    return diffArr;
};