Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

Itersection of Multiple Arrays

node v14.20.1
version: 1.0.0
endpointsharetweet
Shopee SG 前端面试算法题 ``` Itersection of Multiple Arrays Input: nums = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]] Output: [3,4] Input: nums = [[1,2,3],[4,5,6]] Output: [] ``` 实现一个时间复杂度为 O(n) 的算法
const sovle = (input) => { const hashTable = {}; for(let child of input){ for(let item of child){ if(hashTable[item]){ hashTable[item] = hashTable[item] + 1; continue; } hashTable[item] = 1; } } return Object.entries(hashTable).filter(([,times])=>times === input.length).map(([key])=>Number(key)); } const input1 = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]]; const input2 = [[1,2,3],[4,5,6]]; console.log(sovle(input1)); // [3,4] console.log(sovle(input2)); // []
Loading…

no comments

    sign in to comment