Clone and edit this document
Runkit
Runkit
home page
user forum
new notebook
clone notebook
download notebook
support & documentation
log in
sign up
new notebook
help & feedback
clone this notebook
download this notebook
Sign In
Sign Up
Itersection of Multiple Arrays
node v14.20.1
version:
1.0.0
endpoint
share
tweet
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