Just for fun:
const list = [ [1, 5, 10, 20, 40, 80], [6, 7, 20, 80, 100], [3, 4, 15, 20, 30, 70, 80, 120], ]; console.log(intersectList(list)); // [20, 80] function intersect(last, values) { const collectCommon = (common, value) => last.has(value) ? common.add(value) : common; return values.reduce(collectCommon, new Set()); } function intersectList(list) { const [head, ...tail] = list; if (tail.length === 0) return typeof head === 'undefined' ? [] : head; return [...tail.reduce(intersect, new Set(head))]; }
For the more imperatively minded:
function intersectList(list) { const [head, ...tail] = list; if (tail.length === 0) return typeof head === 'undefined' ? [] : head; let last = new Set(head); for (const xs of tail) { const common = new Set(); for (const x of xs) if (last.has(x)) common.add(x); last = common; } return [...last]; }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Just for fun:
For the more imperatively minded: