Reputation: 25213
I have two JavaScript arrays:
var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];
I want the output to be:
var array3 = ["Vijendra","Singh","Shakya"];
The output array should have repeated words removed.
How do I merge two arrays in JavaScript so that I get only the unique items from each array in the same order they were inserted into the original arrays?
Upvotes: 2026
Views: 2203089
Reputation: 1045
let firstArray = [
{name: "A", valueOne: 10, valueTwo: 15},
{name: "B", valueOne: 7, valueTwo: 3},
{name: "C", valueOne: 11, valueTwo: 12}
]
let secondArray = [
{name: "D", valueOne: 5, valueTwo: 1},
{name: "E", valueOne: 2, valueTwo: 23},
{name: "C", valueOne: 9, valueTwo: 17},
{name: "B", valueOne: 1, valueTwo: 19},
{name: "A", valueOne: 30, valueTwo: 20}
]
const newArray = secondArray.filter(({ name }) => !firstArray.some((e) => e.name === name))
console.log(newArray)
OP = [
{ name: 'D', valueOne: 5, valueTwo: 1 },
{ name: 'E', valueOne: 2, valueTwo: 23 }
]
Upvotes: 0
Reputation: 67232
To just merge the arrays (without removing duplicates)
Array.concat
:var array1 = ["Vijendra", "Singh"];
var array2 = ["Singh", "Shakya"];
array1 = array1.concat(array2);
console.log(array1);
The original answer was from years ago. ES6 is fully supported and IE is finally dead. Here's the simplest way to merge primitive and object arrays:
const merge = (a, b, predicate = (a, b) => a === b) => {
const c = [...a]; // copy to avoid side effects
// add all items from B to copy C if they're not already present
b.forEach((bItem) => (c.some((cItem) => predicate(bItem, cItem)) ? null : c.push(bItem)))
return c;
}
merge(['a', 'b', 'c'], ['c', 'x', 'd']);
// => ['a', 'b', 'c', 'x', 'd']
merge([{id: 1}, {id: 2}], [{id: 2}, {id: 3}], (a, b) => a.id === b.id);
// [{id: 1}, {id: 2}, {id: 3}]
const array1 = ["Vijendra","Singh"];
const array2 = ["Singh", "Shakya"];
const array3 = [...array1, ...array2];
Since there is no 'built in' way to remove duplicates (ECMA-262 actually has Array.forEach
which would be great for this), we have to do it manually. Note that this pollutes the Array
prototype, use with caution.
Array.prototype.unique = function() {
var a = this.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
};
Then, to use it:
var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];
// Merges both arrays and gets unique items
var array3 = array1.concat(array2).unique();
This will also preserve the order of the arrays (i.e, no sorting needed).
Since many people are annoyed about prototype augmentation of Array.prototype
and for in
loops, here is a less invasive way to use it:
function arrayUnique(array) {
var a = array.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
}
var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];
// Merges both arrays and gets unique items
var array3 = arrayUnique(array1.concat(array2));
For those who are fortunate enough to work with browsers where ES5 is available, you can use Object.defineProperty
like this:
Object.defineProperty(Array.prototype, 'unique', {
enumerable: false,
configurable: false,
writable: false,
value: function() {
var a = this.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
}
});
Upvotes: 2341
Reputation: 688
My quick take
const arePropsEqualDefault = (oldProps, newProps) => {
const uniqueKeys = Object.values({...Object.keys(oldProps), ...Object.keys(newProps)});
for (const key in uniqueKeys) {
console.log(Object.is(oldProps[key], newProps[key]))
if (!Object.is(oldProps[key], newProps[key])) return false;
}
return true;
}
Upvotes: 0
Reputation: 113
We can concat method use to add two or more array
array1.concat(array2, array3, ..., arrayX)
// unique remove duplicates of array
console.log([...new Set([1, 2, 4, 4, 3])]); // [1, 2, 4, 3]
Upvotes: 1
Reputation: 27812
Using Array.prototype.flat()
:
const input = [
[1, 2, 3, 1],
[101, 2, 1, 10],
[2, 1]
];
const union = (arr) => {
return [ ...new Set( arr.flat() ) ];
}
console.log('output', union(input));
Upvotes: 1
Reputation: 602
return an array of unique objects from arrays...
function uniqueObjectArray(baseArray, mergeArray) {
// we can't compare unique objects within an array ~es6...
// ...e.g. concat/destructure/Set()
// so we'll create a mapping of: item.id* for each -> item
const uniqueMap = new Map()
const uniqueArray = []
// hash array items by id*
baseArray.forEach(item => !uniqueMap.has(item.id) && uniqueMap.set(item.id, item))
mergeArray.forEach(item => !uniqueMap.has(item.id) && uniqueMap.set(item.id, item))
// hash -> array
uniqueMap.forEach(item => uniqueArray.push(item))
return uniqueArray
}
Upvotes: 0
Reputation: 429
EDIT:
The first solution is the fastest only when there are few items. When there are over 400 items, the Set
solution becomes the fastest. And when there are 100,000 items, it is a thousand times faster than the first solution.
Considering that performance is important only when there is a lot of items, and that the Set
solution is by far the most readable, it should be the right solution in most cases
The perf results below were computed with a small number of items
Based on jsperf, the fastest way (edit: if there are less than 400 items) to merge two arrays in a new one is the following:
for (var i = 0; i < array2.length; i++)
if (array1.indexOf(array2[i]) === -1)
array1.push(array2[i]);
This one is 17% slower:
array2.forEach(v => array1.includes(v) ? null : array1.push(v));
This one is 45% slower (edit: when there is less than 100 items. It is a lot faster when there is a lot of items):
var a = [...new Set([...array1 ,...array2])];
And the accepted answer's is 55% slower (and much longer to write) (edit: and it is several order of magnitude slower than any of the other methods when there are 100,000 items)
var a = array1.concat(array2);
for (var i = 0; i < a.length; ++i) {
for (var j = i + 1; j < a.length; ++j) {
if (a[i] === a[j])
a.splice(j--, 1);
}
}
Upvotes: 24
Reputation: 1431
Here is a slightly different take on the loop. With some of the optimizations in the latest version of Chrome, it is the fastest method for resolving the union of the two arrays (Chrome 38.0.2111).
JSPerf: "Merge two arrays keeping only unique values" (archived)
var array1 = ["Vijendra", "Singh"];
var array2 = ["Singh", "Shakya"];
var array3 = [];
var arr = array1.concat(array2),
len = arr.length;
while (len--) {
var itm = arr[len];
if (array3.indexOf(itm) === -1) {
array3.unshift(itm);
}
}
while loop: ~589k ops/s
filter: ~445k ops/s
lodash: 308k ops/s
for loops: 225k ops/s
A comment pointed out that one of my setup variables was causing my loop to pull ahead of the rest because it didn't have to initialize an empty array to write to. I agree with that, so I've rewritten the test to even the playing field, and included an even faster option.
JSPerf: "Merge two arrays keeping only unique values" (archived)
let whileLoopAlt = function (array1, array2) {
const array3 = array1.slice(0);
let len1 = array1.length;
let len2 = array2.length;
const assoc = {};
while (len1--) {
assoc[array1[len1]] = null;
}
while (len2--) {
let itm = array2[len2];
if (assoc[itm] === undefined) { // Eliminate the indexOf call
array3.push(itm);
assoc[itm] = null;
}
}
return array3;
};
In this alternate solution, I've combined one answer's associative array solution to eliminate the .indexOf()
call in the loop which was slowing things down a lot with a second loop, and included some of the other optimizations that other users have suggested in their answers as well.
The top answer here with the double loop on every value (i-1) is still significantly slower. lodash is still doing strong, and I still would recommend it to anyone who doesn't mind adding a library to their project. For those who don't want to, my while loop is still a good answer and the filter answer has a very strong showing here, beating out all on my tests with the latest Canary Chrome (44.0.2360) as of this writing.
Check out Mike's answer and Dan Stocker's answer if you want to step it up a notch in speed. Those are by far the fastest of all results after going through almost all of the viable answers.
Upvotes: 43
Reputation: 1660
you can use new Set to remove duplication
[...new Set([...array1 ,...array2])]
Upvotes: 11
Reputation: 2490
let array1 = [1, 2, 3, 4, 5]
let array2 = [1, 4, 6, 9]
// Using array.concat and array.filter
const array3 = array1.concat(array2.filter((item)=> array1.indexOf(item) == -1 ))
console.log('array3 : ', array3);
// Using new Set and Spread Operator
const array4 = [...new Set([...array1 ,...array2])];
console.log('array4 : ', array4);
// Using array.concat and new Set
const array5 = [...new Set(array1.concat(array2))];
console.log('array5 : ', array5);
Upvotes: 3
Reputation: 573
I have a similar request but it is with Id of the elements in the array.
And, here is the way I do the deduplication.
It is simple, easy to maintain, and good to use.
// Vijendra's Id = Id_0
// Singh's Id = Id_1
// Shakya's Id = Id_2
let item0 = { 'Id': 'Id_0', 'value': 'Vijendra' };
let item1 = { 'Id': 'Id_1', 'value': 'Singh' };
let item2 = { 'Id': 'Id_2', 'value': 'Shakya' };
let array = [];
array = [ item0, item1, item1, item2 ];
let obj = {};
array.forEach(item => {
obj[item.Id] = item;
});
let deduplicatedArray = [];
let deduplicatedArrayOnlyValues = [];
for(let [index, item] of Object.values(obj).entries()){
deduplicatedArray = [ ...deduplicatedArray, item ];
deduplicatedArrayOnlyValues = [ ...deduplicatedArrayOnlyValues , item.value ];
};
console.log( JSON.stringify(array) );
console.log( JSON.stringify(deduplicatedArray) );
console.log( JSON.stringify(deduplicatedArrayOnlyValues ) );
The console log
[{"recordId":"Id_0","value":"Vijendra"},{"recordId":"Id_1","value":"Singh"},{"recordId":"Id_1","value":"Singh"},{"recordId":"Id_2","value":"Shakya"}]
[{"recordId":"Id_0","value":"Vijendra"},{"recordId":"Id_1","value":"Singh"},{"recordId":"Id_2","value":"Shakya"}]
["Vijendra","Singh","Shakya"]
Upvotes: 0
Reputation: 2312
Not Performant if you have extremely large lists, and this isnt for merging since many solutions already have been documented, but i solved my problems with this solution (since most solutions of array filtering apply to simple arrays)
const uniqueVehiclesServiced =
invoice.services.sort().filter(function(item, pos, ary) {
const firstIndex = invoice.services.findIndex((el, i, arr) => el.product.vin === item.product.vin)
return !pos || firstIndex == pos;
});
Upvotes: 0
Reputation: 25820
To offer something simpler and more elegant, in this day and age, using an existing library:
import {pipe, concat, distinct} from 'iter-ops';
// our inputs:
const array1 = ['Vijendra', 'Singh'];
const array2 = ['Singh', 'Shakya'];
const i = pipe(
array1,
concat(array2), // adding array
distinct() // making it unique
);
console.log([...i]); //=> ['Vijendra', 'Singh', 'Shakya']
It is both high performance, as we are iterating only once, and the code is very easy to read.
P.S. I'm the author of iter-ops.
Upvotes: 1
Reputation: 5010
The ES6 offers a single-line solution for merging multiple arrays without duplicates by using destructuring and set.
const array1 = ['a','b','c'];
const array2 = ['c','c','d','e'];
const array3 = [...new Set([...array1,...array2])];
console.log(array3); // ["a", "b", "c", "d", "e"]
Upvotes: 35
Reputation: 8626
Care about efficiency, yet want to do it inline
const s = new Set(array1);
array2.forEach(a => s.add(a));
const merged_array = [...s]; // optional: convert back in array type
Upvotes: 2
Reputation: 93163
[...array1,...array2] // => don't remove duplication
OR
[...new Set([...array1 ,...array2])]; // => remove duplication
Upvotes: 467
Reputation: 20061
I simplified the best of this answer and turned it into a nice function:
function mergeUnique(arr1, arr2){
return arr1.concat(arr2.filter(function (item) {
return arr1.indexOf(item) === -1;
}));
}
Upvotes: 40
Reputation: 10361
Here is another neat solution using Set
:
const o1 = {a: 1};
const arr1 = ['!@#$%^&*()', 'gh', 123, o1, 1, true, undefined, null];
const arr2 = ['!@#$%^&*()', 123, 'abc', o1, 0x001, true, void 0, 0];
const mergeUnique = (...args) => [ ...new Set([].concat(...args)) ];
console.log(mergeUnique(arr1, arr2));
Upvotes: 2
Reputation: 92347
Today 2020.10.15 I perform tests on MacOs HighSierra 10.13.6 on Chrome v86, Safari v13.1.2 and Firefox v81 for chosen solutions.
For all browsers
I perform 2 tests cases:
on solutions A, B, C, D, E, G, H, J, L, M presented in below snippet
// https://stackoverflow.com/a/10499519/860099
function A(arr1,arr2) {
return _.union(arr1,arr2)
}
// https://stackoverflow.com/a/53149853/860099
function B(arr1,arr2) {
return _.unionWith(arr1, arr2, _.isEqual);
}
// https://stackoverflow.com/a/27664971/860099
function C(arr1,arr2) {
return [...new Set([...arr1,...arr2])]
}
// https://stackoverflow.com/a/48130841/860099
function D(arr1,arr2) {
return Array.from(new Set(arr1.concat(arr2)))
}
// https://stackoverflow.com/a/23080662/860099
function E(arr1,arr2) {
return arr1.concat(arr2.filter((item) => arr1.indexOf(item) < 0))
}
// https://stackoverflow.com/a/28631880/860099
function G(arr1,arr2) {
var hash = {};
var i;
for (i = 0; i < arr1.length; i++) {
hash[arr1[i]] = true;
}
for (i = 0; i < arr2.length; i++) {
hash[arr2[i]] = true;
}
return Object.keys(hash);
}
// https://stackoverflow.com/a/13847481/860099
function H(a, b){
var hash = {};
var ret = [];
for(var i=0; i < a.length; i++){
var e = a[i];
if (!hash[e]){
hash[e] = true;
ret.push(e);
}
}
for(var i=0; i < b.length; i++){
var e = b[i];
if (!hash[e]){
hash[e] = true;
ret.push(e);
}
}
return ret;
}
// https://stackoverflow.com/a/1584377/860099
function J(arr1,arr2) {
function arrayUnique(array) {
var a = array.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
}
return arrayUnique(arr1.concat(arr2));
}
// https://stackoverflow.com/a/25120770/860099
function L(array1, array2) {
const array3 = array1.slice(0);
let len1 = array1.length;
let len2 = array2.length;
const assoc = {};
while (len1--) {
assoc[array1[len1]] = null;
}
while (len2--) {
let itm = array2[len2];
if (assoc[itm] === undefined) { // Eliminate the indexOf call
array3.push(itm);
assoc[itm] = null;
}
}
return array3;
}
// https://stackoverflow.com/a/39336712/860099
function M(arr1,arr2) {
const comp = f => g => x => f(g(x));
const apply = f => a => f(a);
const flip = f => b => a => f(a) (b);
const concat = xs => y => xs.concat(y);
const afrom = apply(Array.from);
const createSet = xs => new Set(xs);
const filter = f => xs => xs.filter(apply(f));
const dedupe = comp(afrom) (createSet);
const union = xs => ys => {
const zs = createSet(xs);
return concat(xs) (
filter(x => zs.has(x)
? false
: zs.add(x)
) (ys));
}
return union(dedupe(arr1)) (arr2)
}
// -------------
// TEST
// -------------
var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];
[A,B,C,D,E,G,H,J,L,M].forEach(f=> {
console.log(`${f.name} [${f([...array1],[...array2])}]`);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>
This snippet only presents functions used in performance tests - it not perform tests itself!
And here are example test run for chrome
UPDATE
I remove cases F,I,K because they modify input arrays and benchmark gives wrong results
Upvotes: 19
Reputation: 2399
Built a tester to check just how fast some of the performance oriented answers are. Feel free to add some more. So far, Set
is both the simplest and fastest option (by bigger margins as the number of records increases), at least with simple Number
types.
const records = 10000, //max records per array
max_int = 100, //max integer value per array
dup_rate = .5; //rate of duplication
let perf = {}, //performance logger,
ts = 0,
te = 0,
array1 = [], //init arrays
array2 = [],
array1b = [],
array2b = [],
a = [];
//populate randomized arrays
for (let i = 0; i < records; i++) {
let r = Math.random(),
n = r * max_int;
if (Math.random() < .5) {
array1.push(n);
r < dup_rate && array2.push(n);
} else {
array2.push(n);
r < dup_rate && array1.push(n);
}
}
//simple deep copies short of rfdc, in case someone wants to test with more complex data types
array1b = JSON.parse(JSON.stringify(array1));
array2b = JSON.parse(JSON.stringify(array2));
console.log('Records in Array 1:', array1.length, array1b.length);
console.log('Records in Array 2:', array2.length, array2b.length);
//test method 1 (jsperf per @Pitouli)
ts = performance.now();
for (let i = 0; i < array2.length; i++)
if (array1.indexOf(array2[i]) === -1)
array1.push(array2[i]); //modifies array1
te = performance.now();
perf.m1 = te - ts;
console.log('Method 1 merged', array1.length, 'records in:', perf.m1);
array1 = JSON.parse(JSON.stringify(array1b)); //reset array1
//test method 2 (classic forEach)
ts = performance.now();
array2.forEach(v => array1.includes(v) ? null : array1.push(v)); //modifies array1
te = performance.now();
perf.m2 = te - ts;
console.log('Method 2 merged', array1.length, 'records in:', perf.m2);
//test method 3 (Simplest native option)
ts = performance.now();
a = [...new Set([...array1, ...array2])]; //does not modify source arrays
te = performance.now();
perf.m3 = te - ts;
console.log('Method 3 merged', a.length, 'records in:', perf.m3);
//test method 4 (Selected Answer)
ts = performance.now();
a = array1.concat(array2); //does not modify source arrays
for (let i = 0; i < a.length; ++i) {
for (let j = i + 1; j < a.length; ++j) {
if (a[i] === a[j])
a.splice(j--, 1);
}
}
te = performance.now();
perf.m4 = te - ts;
console.log('Method 4 merged', a.length, 'records in:', perf.m4);
//test method 5 (@Kamil Kielczewski)
ts = performance.now();
function K(arr1, arr2) {
let r = [],
h = {};
while (arr1.length) {
let e = arr1.shift(); //modifies array1
if (!h[e]) h[e] = 1 && r.push(e);
}
while (arr2.length) {
let e = arr2.shift(); //modifies array2
if (!h[e]) h[e] = 1 && r.push(e);
}
return r;
}
a = K(array1, array2);
te = performance.now();
perf.m5 = te - ts;
console.log('Method 5 merged', a.length, 'records in:', perf.m4);
array1 = JSON.parse(JSON.stringify(array1b)); //reset array1
array2 = JSON.parse(JSON.stringify(array2b)); //reset array2
for (let i = 1; i < 6; i++) {
console.log('Method:', i, 'speed is', (perf['m' + i] / perf.m1 * 100).toFixed(2), '% of Method 1');
}
Upvotes: 1
Reputation: 317
For n arrays, you can get the union like so.
function union(arrays) {
return new Set(arrays.flat()).keys();
};
Upvotes: 1
Reputation: 6718
Here an option for objects with object arrays:
const a = [{param1: "1", param2: 1},{param1: "2", param2: 2},{param1: "4", param2: 4}]
const b = [{param1: "1", param2: 1},{param1: "4", param2: 5}]
var result = a.concat(b.filter(item =>
!JSON.stringify(a).includes(JSON.stringify(item))
));
console.log(result);
//Result [{param1: "1", param2: 1},{param1: "2", param2: 2},{param1: "4", param2: 4},{param1: "4", param2: 5}]
Upvotes: 4
Reputation: 702
Just steer clear of nested loops (O(n^2)), and .indexOf()
(+O(n)).
function merge(a, b) {
var hash = {};
var i;
for (i = 0; i < a.length; i++) {
hash[a[i]] = true;
}
for (i = 0; i < b.length; i++) {
hash[b[i]] = true;
}
return Object.keys(hash);
}
var array1 = ["Vijendra", "Singh"];
var array2 = ["Singh", "Shakya"];
var array3 = merge(array1, array2);
console.log(array3);
Upvotes: 29
Reputation: 4501
If you merging object arrays, consider use of lodash UnionBy
function, it allows you to set custom predicate compare objects:
import { unionBy } from 'lodash';
const a = [{a: 1, b: 2}];
const b = [{a: 1, b: 3}];
const c = [{a: 2, b: 4}];
const result = UnionBy(a,b,c, x => x.a);
Result is: [{ a: 1; b: 2 }, { a: 2; b: 4 }]
First passed match from arrays is used in result
Upvotes: 3
Reputation: 8121
I know this question is not about array of objects, but searchers do end up here.
so it's worth adding for future readers a proper ES6 way of merging and then removing duplicates
array of objects:
var arr1 = [ {a: 1}, {a: 2}, {a: 3} ];
var arr2 = [ {a: 1}, {a: 2}, {a: 4} ];
var arr3 = arr1.concat(arr2.filter( ({a}) => !arr1.find(f => f.a == a) ));
// [ {a: 1}, {a: 2}, {a: 3}, {a: 4} ]
Upvotes: 32
Reputation: 1527
There are so many solutions for merging two arrays. They can be divided into two main categories(except the use of 3rd party libraries like lodash or underscore.js).
a) combine two arrays and remove duplicated items.
b) filter out items before combining them.
// mutable operation(array1 is the combined array)
array1.push(...array2);
array1.unshift(...array2);
// immutable operation
const combined = array1.concat(array2);
const combined = [...array1, ...array2]; // ES6
There are many ways to unifying an array, I personally suggest below two methods.
// a little bit tricky
const merged = combined.filter((item, index) => combined.indexOf(item) === index);
const merged = [...new Set(combined)];
There are also many ways, but I personally suggest the below code due to its simplicity.
const merged = array1.concat(array2.filter(secItem => !array1.includes(secItem)));
Upvotes: 10
Reputation: 54782
Using a Set (ECMAScript 2015), it will be as simple as that:
const array1 = ["Vijendra", "Singh"];
const array2 = ["Singh", "Shakya"];
console.log(Array.from(new Set(array1.concat(array2))));
Upvotes: 154
Reputation: 9031
This is an ECMAScript 6 solution using spread operator and array generics.
Currently it only works with Firefox, and possibly Internet Explorer Technical Preview.
But if you use Babel, you can have it now.
const input = [
[1, 2, 3],
[101, 2, 1, 10],
[2, 1]
];
const mergeDedupe = (arr) => {
return [...new Set([].concat(...arr))];
}
console.log('output', mergeDedupe(input));
Upvotes: 289
Reputation: 11
//1.merge two array into one array
var arr1 = [0, 1, 2, 4];
var arr2 = [4, 5, 6];
//for merge array we use "Array.concat"
let combineArray = arr1.concat(arr2); //output
alert(combineArray); //now out put is 0,1,2,4,4,5,6 but 4 reapeat
//2.same thing with "Spread Syntex"
let spreadArray = [...arr1, ...arr2];
alert(spreadArray); //now out put is 0,1,2,4,4,5,6 but 4 reapete
/*
if we need remove duplicate element method use are
1.Using set
2.using .filter
3.using .reduce
*/
Upvotes: 0