-
Notifications
You must be signed in to change notification settings - Fork 1
/
containsDuplicate.js
46 lines (44 loc) · 1.09 KB
/
containsDuplicate.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
/**
* Runtime efficient version.
* I never learned what a set is until now: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set?retiredLocale=de
* A set works good in this case, because it can never have an element twice.
*
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function(nums) {
let helperarr = new Set();
for (const element of nums){
if(helperarr.has(element))
return true;
else
helperarr.add(element);
}
return false;
};
/**
* A solution that is 50/50 between memory and runtime
*
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate3 = function(nums) {
let set = new Set(nums);
return set.size != nums.length;
};
/**
* Memory efficient version
*
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate2 = function(nums) {
let helperarr = [];
for (let i = 0; i < nums.length; i++){
if(helperarr.includes(nums[i]))
return true;
else
helperarr[i] = nums[i];
}
return false;
};