Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

217. 存在重复元素 #41

Open
webVueBlog opened this issue Aug 31, 2022 · 0 comments
Open

217. 存在重复元素 #41

webVueBlog opened this issue Aug 31, 2022 · 0 comments

Comments

@webVueBlog
Copy link
Owner

217. 存在重复元素

Description

Difficulty: 简单

Related Topics: 数组, 哈希表, 排序

给你一个整数数组 nums 。如果任一值在数组中出现 至少两次 ,返回 true ;如果数组中每个元素互不相同,返回 false

示例 1:

输入:nums = [1,2,3,1]
输出:true

示例 2:

输入:nums = [1,2,3,4]
输出:false

示例 3:

输入:nums = [1,1,1,3,3,4,3,2,4,2]
输出:true

提示:

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109

Solution

Language: JavaScript

/**
 * @param {number[]} nums
 * @return {boolean}
 */
// 排序
// var containsDuplicate = function(nums) {
//     nums.sort((a, b) => a - b)
//     const n = nums.length
//     for (let i = 0; i < n - 1; i++) {
//         if (nums[i] === nums[i + 1]) {
//             return true
//         }
//     }
//     return false
// };


// 哈希表
var containsDuplicate = function(nums) {
    const set = new Set()
    for (let x of nums) {
        if (set.has(x)) return true
        set.add(x)
    }
    return false
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant