What Is the Fastest or Most Elegant Way to Compute a Set Difference Using JavaScript Arrays

What is the fastest or most elegant way to compute a set difference using Javascript arrays?

I don't know if this is most effective, but perhaps the shortest:

var A = [1, 2, 3, 4];
var B = [1, 3, 4, 7];

var diff = A.filter(function(x) {
return B.indexOf(x) < 0;
});

console.log(diff); // [2]

Most efficient way to compare multiple arrays based on a fixed element and copy the largest one?

You can use sort and just return the first element of descending sorted list

const arr = [this.k1, this.k2, this.k3, this.k4]
// sort desc
const arrayHasMaxLastElement = arr.sort((a, b) => b[b.length - 1] - a[a.length - 1])[0]

How to get the difference between two arrays in JavaScript?

This answer was written in 2009, so it is a bit outdated, also it's rather educational for understanding the problem. Best solution I'd use today would be

let difference = arr1.filter(x => !arr2.includes(x));

(credits to other author here)

I assume you are comparing a normal array. If not, you need to change the for loop to a for .. in loop.

function arr_diff (a1, a2) {

var a = [], diff = [];

for (var i = 0; i < a1.length; i++) {
a[a1[i]] = true;
}

for (var i = 0; i < a2.length; i++) {
if (a[a2[i]]) {
delete a[a2[i]];
} else {
a[a2[i]] = true;
}
}

for (var k in a) {
diff.push(k);
}

return diff;
}

console.log(arr_diff(['a', 'b'], ['a', 'b', 'c', 'd']));
console.log(arr_diff("abcd", "abcde"));
console.log(arr_diff("zxc", "zxc"));

Find the differences between 2 multidimensional arrays

Assuming there is some character (e.g. '_') that cannot appear in the strings, you may convert your problem to dealing with arrays of strings, for example as follows:
(I didn't compact the code, to keep some readability)

set1 = new Set(arr1.map(x => x[0]+'_'+x[1]))
set2 = new Set(arr2.map(x => x[0]+'_'+x[1]))
diff1 = arr1.filter(x => !set2.has(x[0]+'_'+x[1]))
diff2 = arr2.filter(x => !set1.has(x[0]+'_'+x[1]))
res = diff1.concat(diff2)

Compare 2 arrays in javascript and extract differences

your requirement(result) is not clear, but this will get you started.

var arr1 = [{ "329": ["45738", "45737", "45736"] }, { "307": ["45467", "45468"] }, { "355": ["47921"] }],  arr2 = [{ "355": ["47921", "45922"] }, { "329": ["45738", "45737", "45736"] }, { "307": [] }];
var result = [];
arr2.forEach(obj => { var key = Object.keys(obj)[0]; var match = arr1.find(o => o.hasOwnProperty(key)); if (match) { var newObj = {}; newObj[key] = obj[key].filter(s => match[key].indexOf(s) === -1); if (!obj[key].length || newObj[key].length) result.push(newObj) } else { result.push(Object.assign({}, obj)); }});
console.log(result);

Function that returns difference of two arrays of strings in javascript

function diff(A, B) {
return A.filter(function (a) {
return B.indexOf(a) == -1;
});
}

What is the fastest or most elegant way to compute a set difference using Javascript arrays?

I don't know if this is most effective, but perhaps the shortest:

var A = [1, 2, 3, 4];
var B = [1, 3, 4, 7];

var diff = A.filter(function(x) {
return B.indexOf(x) < 0;
});

console.log(diff); // [2]


Related Topics



Leave a reply



Submit