You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
704 B
JavaScript
30 lines
704 B
JavaScript
6 years ago
|
function consolidateLists(lists, threshold = 1){
|
||
|
if (typeof threshold !== 'number') {
|
||
|
throw Error('Provided threshold is not a number');
|
||
|
}
|
||
|
|
||
|
// calculate list size manually since `Set`
|
||
|
// does not have a `length` attribute
|
||
|
let listSize = 0;
|
||
|
const occurences = {};
|
||
|
lists.forEach(list => {
|
||
|
listSize += 1;
|
||
|
list.forEach(item => {
|
||
|
if (!(item in occurences)) {
|
||
|
occurences[item] = 1;
|
||
|
} else {
|
||
|
occurences[item] += 1;
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
|
||
|
const scaledThreshold = listSize * threshold;
|
||
|
return Object.entries(occurences)
|
||
|
.filter(keyValue => keyValue[1] >= scaledThreshold)
|
||
|
.map(keyValue => keyValue[0]);
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
consolidateLists,
|
||
|
}
|