Created
August 13, 2020 16:21
-
-
Save Richienb/38198873399b5799271654e1c2915e49 to your computer and use it in GitHub Desktop.
Finding distinct items in an array - See https://stackoverflow.com/questions/63396741/how-do-i-get-the-numbers-that-do-not-have-a-duplicated-value-in-an-array-of-numb/63396901 (https://jsbench.github.io/#38198873399b5799271654e1c2915e49) #jsbench #jsperf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Finding distinct items in an array - See https://stackoverflow.com/questions/63396741/how-do-i-get-the-numbers-that-do-not-have-a-duplicated-value-in-an-array-of-numb/63396901</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
const rand = n => Math.floor(Math.random() * n) | |
const inrange = (min, max) => rand(max - min + 1) + min | |
const array = Array.from(Array(1e6), _ => inrange(1e6, 1e7 - 1)) | |
const solutionOne = arr => { | |
const freq = arr.reduce((acc, curr) => (acc[curr] = (acc[curr] || 0) + 1, acc), {}); | |
return arr.filter(x => freq[x] === 1); | |
} | |
const solutionTwo = arr => { | |
const freq = arr.reduce((map, curr) => (map.set(curr, (map.get(curr) || 0) + 1), map), new Map); | |
return arr.filter(x => freq.get(x) === 1); | |
} | |
const solutionThree = values => { | |
const r = new Set(values) // result | |
const s = new Set // seen | |
for (const v of values) | |
if (s.has(v)) | |
r.delete(v) | |
else | |
s.add(v) | |
return Array.from(r) | |
} | |
const solutionFour = values => { | |
const r = new Set // result | |
const s = new Set // seen | |
for (const v of values) | |
if (s.has(v)) | |
continue | |
else if (r.has(v)) | |
(r.delete(v), s.add(v)) | |
else | |
r.add(v) | |
return Array.from(r) | |
} | |
const solutionFive = og => { | |
const ogfrequency = {}; | |
/*compute frequency of each elem*/ | |
for (const i of og) { | |
ogfrequency[i] = ogfrequency[i] ? ++ogfrequency[i] : 1; | |
} | |
/*pass or fail the test of filter based on frequencies collected previously*/ | |
const answer = og.filter((e) => ogfrequency[e] === 1); | |
return answer | |
} | |
const solutionSix = values => values.filter(value => | |
values.filter(v => v === value).length === 1 | |
); | |
const arrayDistinct = array => array.filter(value => array.indexOf(value) === array.lastIndexOf(value)) | |
}; | |
suite.add("solutionTwo(array)", function () { | |
solutionTwo(array) | |
}); | |
suite.add("solutionThree(array)", function () { | |
solutionThree(array) | |
}); | |
suite.add("solutionFour(array)", function () { | |
solutionFour(array) | |
}); | |
suite.add("solutionFive(array)", function () { | |
solutionFive(array) | |
}); | |
suite.add("arrayDistinct(array)", function () { | |
arrayDistinct(array) | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("Finding distinct items in an array - See https://stackoverflow.com/questions/63396741/how-do-i-get-the-numbers-that-do-not-have-a-duplicated-value-in-an-array-of-numb/63396901"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment