λ¬Έμ
Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:
- answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
- answer[1] is a list of all distinct integers in nums2 which are not present in nums1.
Note that the integers in the lists may be returned in any order.
Example 1:
Input: nums1 = [1,2,3], nums2 = [2,4,6]
Output: [[1,3],[4,6]]
Explanation:
For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3].
For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6].
ν΄κ²°λ°©ν₯
μ£Όμ΄μ§ λ κ°μ λ°°μ΄μμ κ΅μ§ν©μ μ°Ύμλ΄μ κ°κ°μ λ°°μ΄μμ κ΅μ§ν©λ§ μ κ±°ν΄μ£Όλ©΄ λμ§ μλ? λΌκ³ μκ°μ νλ€.
κ·Έλμ μ²μμ μμ±νλ μ λͺ» λ ν΄κ²°λ²μ μλμ μλ€. μ΄μμ μλμ μ½λλ μλ. ν μ€νΈ μ½λ λͺκ°λ ν΅κ³Όνκ³ λͺ κ°λ ν΅κ³Ό λͺ» ν΄μ λ€μ μμ±νλ€κ° λ³κ²½λκ² μλμ κ°μ. μ€λ³΅λλ κ΅μ§ν©μ΄ μμμλ μλ€λ μ¬μ€μ μκ° λͺ» ν¨.
let duplicated = []
nums1 = new Set(nums1);
nums2 = new Set(nums2);
const n1 = Array.from(nums1)
const n2 = Array.from(nums2)
for(let i=0; i<n1.length; i++){
for(let j=0; j<n2.length; j++ ){
if(n1[i] == n2[j]){
duplicated.push(n1[i])
}
}
}
const set = new Set(duplicated);
const dupArr = [...set]
console.log(dupArr)
const temp1 = n1.filter((v) => dupArr.some((item) => item !== v ? v : ''))
const temp2 = n2.filter((v) => dupArr.some((item) => item !== v ? v : ''))
console.log(temp1, temp2)
return [temp1, temp2]
μ루μ
var findDifference = function(nums1, nums2) {
const set1 = new Set(nums1);
const set2 = new Set(nums2);
return [
Array.from(set1).filter(n => !set2.has(n)),
Array.from(set2).filter(n => !set1.has(n)),
]
};
Set.prototype.has() μ μ¬μ©νλ©΄ Set κ°μ²΄μ μ£Όμ΄μ§ μμκ° μ‘΄μ¬νλμ§ μ¬λΆλ₯Ό νλ³ν΄ λ°νν©λλ€.
κ·Έλμ set1 λ°°μ΄ μμλ₯Ό νλνλ set2μ λμ ν΄μ μλ μλ νλ³ν΄μ μλ κ²λ§ filterνκ² ν©λλ€. μμλ set2λμ