-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0217-contains-duplicate.js
More file actions
68 lines (56 loc) · 1.66 KB
/
Copy path0217-contains-duplicate.js
File metadata and controls
68 lines (56 loc) · 1.66 KB
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
//Blog: https://www.allenliservice.online/leetcode-js-217-contains-duplicate/
// <strong>Solution:</strong>
// 1. 建立 hashTable 物件。
// 2. 運用「for of」來將 nums 陣列中的數值依序取出。
// 3. 判斷該數值是否存在 hashTable 物件中,如有回傳 true,反之在 hashTable 物件存入該數值為名稱,並配對 true。
// 4. 沒有重複出現的數值,則回傳 false。
// <strong>Code 1: BigO(n)</strong>
var containsDuplicate = function (nums) {
let hashTable = {};
for (let num of nums) {
if (hashTable[num]) return true;
hashTable[num] = true;
}
return false;
};
/* <strong>FlowChart:</strong>
<strong>Example 1</strong>
<pre style='background-color:#ggg'>
Input: nums = [1,2,3,1]
{ '1': true }
{ '1': true, '2': true }
{ '1': true, '2': true, '3': true }
if (hashTable[1]) = true, return true;
</pre> */
// <strong>Code 2: BigO(2n)</strong>
var containsDuplicate = function (nums) {
const hashTable = {};
for (let i = 0; i < nums.length; i++) {
hashTable[nums[i]] = (hashTable[nums[i]] || 0) + 1;
}
for (let num of nums) {
if (hashTable[num] > 1) return true;
}
return false;
};
// <strong>Code 3: BigO(n)</strong>
var containsDuplicate = function (nums) {
const numsSet = new Set();
for (let num of nums) {
if (numsSet.has(num)) {
return true;
} else {
numsSet.add(num);
}
}
return false;
};
// <strong>Code 4: BigO(n)</strong>
var containsDuplicate = function (nums) {
const numsSet = new Set(nums);
return numsSet.size !== nums.length;
};
// <strong>Code 5: BigO(n)</strong>
var containsDuplicate = function (nums) {
return new Set(nums).size !== nums.length;
};