-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathareThereDuplicates.js
More file actions
31 lines (28 loc) · 867 Bytes
/
areThereDuplicates.js
File metadata and controls
31 lines (28 loc) · 867 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
/*
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(...rest) {
// good luck. (supply any arguments you deem necessary.)
var left = 0;
var right = 1;
while (left < right) {
if (rest[left] === rest[right]) {
return true;
} else if (right >= rest.length) {
return false;
} else if (right === rest.length - 1) {
left++;
right = left + 1;
} else {
right++;
}
}
return false;
}