code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

448 Find All Numbers Disappeared In A Array

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const findAllNumbersDisappearedInAArray = nums => {
  for (let i = 0; i < nums.length; i++) {
    const index = Math.abs(nums[i]) - 1
    nums[index] = nums[index] < 0 ? nums[index] : nums[index] * -1
  }
  const result = []
  for (let i = 0; i < nums.length; i++) {
    if (nums[i] > 0) {
      result.push(i + 1)
    }
  }
  return result
}

console.log(findAllNumbersDisappearedInAArray([4, 3, 2, 7, 8, 2, 3, 1]))

Created 2022-03-21T13:47:24+00:00 · Edit