-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicateChecker.js
More file actions
42 lines (38 loc) · 969 Bytes
/
duplicateChecker.js
File metadata and controls
42 lines (38 loc) · 969 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
31
32
33
34
35
36
37
38
39
40
41
42
// using frequency counter
function areThereDuplicates() {
let collection = {};
for (let val in arguments) {
collection[arguments[val]] = (collection[arguments[val]] || 0) + 1;
}
console.log(collection)
for (let key in collection) {
if (collection[key] > 1) return true;
}
return false;
}
console.log("using frequency counter: "+areThereDuplicates(1,5,6,6,3,4,9));
// using multiple pointers
function areThereDuplicates(...args) {
// Two pointers
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(
"using multiple pointers: " + areThereDuplicates(1, 5, 6, 3, 4, 9)
);
// using one liner solution
function areThereDuplicates() {
return new Set(arguments).size !== arguments.length;
}
console.log(
"using one liner solution: " + areThereDuplicates(1, 5, 6, 6, 3, 4, 9)
);