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

217 Contains Duplicates

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

/* 
  Given an array of integers, find if the array contains any duplicates.

  Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

  Example 1:

  Input: [1,2,3,1]
  Output: true
  Example 2:

  Input: [1,2,3,4]
  Output: false
  Example 3:

  Input: [1,1,1,3,3,4,3,2,4,2]
  Output: true
*/

function containsDuplicates(nums) {
  let numsDict = {}
  for (let index = 0; index < nums.length; index++) {
    if(!numsDict.hasOwnProperty(nums[index])) {
      numsDict[nums[index]] = 1
    } else {
      return true
    }
  }
  return false
}

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

Created 2020-01-18T18:20:57+00:00 · Edit