-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0217-contains-duplicate.js
72 lines (61 loc) · 1.77 KB
/
0217-contains-duplicate.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
70
71
72
/**
* Brute Force - Linear Search
* Time O(N^2) | Space O(1)
* https://leetcode.com/problems/contains-duplicate/
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = (nums) => {
for (let right = 0; right < nums.length; right++) {/* Time O(N) */
for (let left = 0; left < right; left++) { /* Time O(N) */
const isDuplicate = nums[left] === nums[right];
if (isDuplicate) return true;
}
}
return false;
}
/**
* Sort - HeapSort Space O(1) | QuickSort Space O(log(N))
* Time O(N * log(N)) | Space O(1)
* https://leetcode.com/problems/contains-duplicate/
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = (nums) => {
nums.sort((a, b) => a - b);/* Time O(N * log(N)) | Space O(1 || log(N)) */
return hasDuplicate(nums);
}
const hasDuplicate = (nums) => {
for (let curr = 0; curr < (nums.length - 1); curr++) {/* Time O(N) */
const next = (curr + 1);
const isNextDuplicate = nums[curr] === nums[next];
if (isNextDuplicate) return true;
}
return false;
}
/**
* Hash Set
* Time O(N) | Space O(N)
* https://leetcode.com/problems/contains-duplicate/
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = (nums) => {
const numsSet = new Set(nums);/* Time O(N) | Space O(N) */
const isEqual = numsSet.size === nums.length;
return !isEqual;
};
/**
* Hash Set - Early Exit
* Time O(N) | Space O(N)
* https://leetcode.com/problems/contains-duplicate/
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = (nums, numsSet = new Set()) => {
for (const num of nums) {/* Time O(N) */
if (numsSet.has(num)) return true;
numsSet.add(num); /* Space O(N) */
}
return false;
};