-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckDuplicates.js
55 lines (48 loc) · 1.55 KB
/
checkDuplicates.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
// Frequency Counter / Multiple Pointers - areThereDuplicates
// Implement a function called, areThereDuplicates which accepts a variable number of arguments, and checks whether there are any duplicates among the arguments passed in. You can solve this using the frequency counter pattern OR the multiple pointers pattern.
// Examples:
// areThereDuplicates(1, 2, 3) // false
// areThereDuplicates(1, 2, 2) // true
// areThereDuplicates('a', 'b', 'c', 'a') // true
function areThereDuplicates(...args) {
let elements = [...args];
let frequencyOfElements = {};
for (let element of elements) {
if (frequencyOfElements[element]) {
return true
} else {
frequencyOfElements[element] = 1;
}
}
return false;
}
//areThereDuplicates Solution (Frequency Counter)
function areThereDuplicates1() {
let collection = {}
for (let val in arguments) {
collection[arguments[val]] = (collection[arguments[val]] || 0) + 1
}
for (let key in collection) {
if (collection[key] > 1) return true
}
return false;
}
//areThereDuplicates Solution (Multiple Pointers)
function areThereDuplicates2(...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
}
//areThereDuplicates One Liner Solution
function areThereDuplicates3() {
return new Set(arguments).size !== arguments.length;
}