title | description | keywords | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
217. 存在重复元素 |
LeetCode 217. 存在重复元素题解,Contains Duplicate,包含解题思路、复杂度分析以及完整的 JavaScript 代码实现。 |
|
🟢 Easy 🔖 数组
哈希表
排序
🔗 力扣
LeetCode
Given an integer array nums
, return true
if any value appears at least twice in the array, and return false
if every element is distinct.
Example 1:
Input: nums = [1,2,3,1]
Output: true
Example 2:
Input: nums = [1,2,3,4]
Output: false
Example 3:
Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true
Constraints:
1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
如果数组里面有重复数字就输出 true
,否则输出 flase
。
用 map
判断即可。
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function (nums) {
const map = new Map();
for (let item of nums) {
if (map.has(item)) return true;
map.set(item, 1);
}
return false;
};
题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
---|---|---|---|---|---|
219 | 存在重复元素 II | [✓] | 数组 哈希表 滑动窗口 |
🟢 | 🀄️ 🔗 |
220 | 存在重复元素 III | 数组 桶排序 有序集合 2+ |
🔴 | 🀄️ 🔗 | |
2357 | 使数组中所有元素都等于零 | [✓] | 贪心 数组 哈希表 3+ |
🟢 | 🀄️ 🔗 |