forked from dnshi/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainsDuplicate.js
More file actions
30 lines (27 loc) · 849 Bytes
/
ContainsDuplicate.js
File metadata and controls
30 lines (27 loc) · 849 Bytes
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
// Source : https://leetcode.com/problems/contains-duplicate/
// Author : Dean Shi
// Date : 2015-06-16
/**********************************************************************************
*
* 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.
*
**********************************************************************************/
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function(nums) {
var hash = {};
for (var i in nums) {
if (!hash[nums[i]]) {
hash[nums[i]] = true;
} else {
return true;
}
}
return false;
};
// Test case
console.log(containsDuplicate([3, 3])); // true