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

001 Contains Duplicates

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

const containsDuplicates = nums => {
  let map = {}
  for (let i = 0; i < nums.length; i++) {
    if (map[nums[i]]) {
      return true
    }
    map[nums[i]] = 1
  }
  return false
}

test('containsDuplicates', () => {
  expect(containsDuplicates([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])).toEqual(true)
  expect(containsDuplicates([1, 2, 3, 1])).toEqual(true)
  expect(containsDuplicates([1, 2, 3, 4])).toEqual(false)
})

Created 2022-01-03T05:25:05+00:00 · Edit