code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

002 Missing Number

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

const missingNumber = nums => {
  let actualSum = 0
  for (const num of nums) {
    actualSum += num
  }
  const totalSum = (nums.length * (nums.length + 1)) / 2
  return totalSum - actualSum
}

test('missingNumber', () => {
  expect(missingNumber([3, 0, 1])).toEqual(2)
  expect(missingNumber([0, 1])).toEqual(2)
  expect(missingNumber([9, 6, 4, 2, 3, 5, 7, 0, 1])).toEqual(8)
})

Created 2022-01-03T06:45:57+00:00 · Edit