-
Notifications
You must be signed in to change notification settings - Fork 0
/
s6-ex4.js
45 lines (33 loc) · 1.01 KB
/
s6-ex4.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
function areThereDuplicates() {
// Time complexity : O(n)
// Space complexity : O(n)
if (arguments.length < 2) return true;
let dups = {};
for (let a in arguments)
{
if (dups[arguments[a]]) return true;
else dups[arguments[a]] = 1;
}
return false;
}
function areThereDuplicatesBonus(...args) {
// Time complexity : O(n log(n))
// Space complexity : O(1)
if (arguments.length < 2) return true;
args.sort( (a,b) => a > b);
let start = 0;
let next = 1;
while (next < args.length)
{
if(args[start] === args[next]) return true;
start++;
next++;
}
return false;
}
// console.log(areThereDuplicates(1, 2, 3)); // False
// console.log(areThereDuplicates(1, 2, 2)); // True
// console.log(areThereDuplicates("a", "b", "c", "a")); // True
console.log(areThereDuplicatesBonus(1, 2, 3)); // False
console.log(areThereDuplicatesBonus(1, 2, 2)); // True
console.log(areThereDuplicatesBonus("a", "b", "c", "a")); // True