-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path217.存在重复元素.js
69 lines (66 loc) · 1.16 KB
/
217.存在重复元素.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* @lc app=leetcode.cn id=217 lang=javascript
*
* [217] 存在重复元素
*
* https://leetcode.cn/problems/contains-duplicate/description/
*
* algorithms
* Easy (55.66%)
* Likes: 749
* Dislikes: 0
* Total Accepted: 588.5K
* Total Submissions: 1.1M
* Testcase Example: '[1,2,3,1]'
*
* 给你一个整数数组 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 <= 10^5
* -10^9 <= nums[i] <= 10^9
*
*
*/
// @lc code=start
/**
* @param {number[]} nums
* @return {boolean}
*/
// var containsDuplicate = function (nums) {
// return nums.length !== [...new Set(nums)].length;
// };
var containsDuplicate = function(nums) {
let map = new Map();
for(let i of nums){
if(map.has(i)){
return true;
}else{
map.set(i, 1);
}
}
return false;
};
// @lc code=end