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

003 Single Number

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

const singleNumber = nums => {
  let result = 0
  for (const num of nums) {
    result ^= num
  }
  return result
}

test('singleNumber', () => {
  expect(singleNumber([2, 2, 1])).toEqual(1)
  expect(singleNumber([4, 1, 2, 1, 2])).toEqual(4)
  expect(singleNumber([1])).toEqual(1)
})

Created 2022-01-03T06:45:57+00:00, updated 2022-01-03T07:04:58+00:00 · History · Edit