Question:

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.

给定整数数组,查找数组是否包含任何重复项。

如果数组中至少出现两次值,则函数应返回true;如果每个元素不同,则函数应返回false

Example:

  1. Input: [1,2,3,1]
  2. Output: true
  3. Input: [1,2,3,4]
  4. Output: false
  5. Input: [1,1,1,3,3,4,3,2,4,2]
  6. Output: true

Solution:

  1. /**
  2. * @param {number[]} nums
  3. * @return {boolean}
  4. */
  5. var containsDuplicate = function(nums) {
  6. return new Set(nums).size < nums.length
  7. };