코딩테스트 공부
[LeetCode] 2032. Two Out of Three - 자바스크립트 0628
codevil
2022. 6. 28. 11:25
https://leetcode.com/problems/two-out-of-three/
Two Out of Three - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
3개의 array에 한번이라도 겹치는 경우가 있다면 그것을 리턴하는 문제이다.
1. 1번에 있는 숫자중에서, 2번 혹은 3번에 있는 숫자를 필터한다
const filteredNum1 = nums1.filter((x) => nums2.includes(x) || nums3.includes(x))
2. 2번에 있는 숫자중에서 3번에 있으나, filteredNum1에는 없는 숫자를 구한다
const filteredNum2 = nums2.filter((x) => nums3.includes(x) && !filteredNum1.includes(x))
3. set리스트에 넣어서 중복을 줄인다
const answer = new Set([...filteredNum1.concat(filteredNum2)])
4. Array로 리턴한다
return Array.from(answer)
전체풀이
var twoOutOfThree = function (nums1, nums2, nums3) {
const filteredNum1 = nums1.filter((x) => nums2.includes(x) || nums3.includes(x))
const filteredNum2 = nums2.filter((x) => nums3.includes(x) && !filteredNum1.includes(x))
const answer = new Set([...filteredNum1.concat(filteredNum2)])
return Array.from(answer)
};