-
Notifications
You must be signed in to change notification settings - Fork 0
/
duplicate.js
31 lines (25 loc) · 871 Bytes
/
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
const getDuplicatesFromArray = function(array){
let duplicates = [];
array.forEach(function(item) {
if(array.filter(val => val === item).length > 1 && duplicates.filter(val => val === item).length === 0){
duplicates.push(item);
}
});
return duplicates;
};
const start = performance.now();
console.log(getDuplicatesFromArray([1,1,3,5,6,3,7]))
const end = performance.now();
const duration = end - start;
console.log(`${duration} milliseconds.`);
const returnDuplicate = (array) => {
const duplicates = [];
const seens = {};
array.forEach((item) => seens[item] ? duplicates.push(item) : seens[item] = true)
return duplicates
}
const start2 = performance.now();
console.log(returnDuplicate([1,1,3,5,6,3,7]))
const end2 = performance.now();
const duration2 = end2 - start2;
console.log(`${duration2} milliseconds.`);