Skip to main content

2215.js

/** ------------------------------------------------------------------------------
*
* 2023-05-03
* 2215. Find the Diffrence of Two Arrays
* https://leetcode.com/problems/find-the-difference-of-two-arrays/submissions/943795871/
*
------------------------------------------------------------------------------ */
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[][]}
*/
var findDifference = function (nums1, nums2) {
const array = nums1.filter((v) => {
for (const s of nums2) {
if (s === v) return false;
}
return true;
});

const array2 = nums2.filter((v) => {
for (const s of nums1) {
if (s === v) return false;
}
return true;
});

return [Array.from(new Set(array)), Array.from(new Set(array2))];
};

var findDifference = function (nums1, nums2) {
let nums1Set = new Set(nums1);
let nums2Set = new Set(nums2);

let result = [[], []];
for (num of nums1Set) {
if (!nums2Set.has(num)) result[0].push(num);
}

for (num of nums2Set) {
if (!nums1Set.has(num)) result[1].push(num);
}

return result;
};

console.log();