댓글 검색 목록

[javascript] 배열의 교차점 얻기

페이지 정보

작성자 운영자 작성일 20-05-11 11:42 조회 619 댓글 0
// space: O(n)
// time: O(n)
const getIntersection = (...arr) => [...(arr.flat().reduce((map, v) => map.set(v, (map.get(v) || 0) + 1), new Map()))].reduce((acc, [v, count]) => void (count === arr.length && acc.push(v)) || acc, []);

// Or
// Only support two arrays
const getIntersection = (a, b) => [...new Set(a)].filter(v => b.includes(v));

// getIntersection([1, 2, 3], [2, 3, 4, 5]) returns [2, 3]
// getIntersection([1, 2, 3], [2, 3, 4, 5], [1, 3, 5]) returns [3]

댓글목록 0

등록된 댓글이 없습니다.

웹학교 로고

온라인 코딩학교

코리아뉴스 2001 - , All right reserved.