Intersection of 2 arrays containing objects

Advertisements

I’m using the below code to intersect 2 very large arrays. Is it possible to improve the performance of this as it takes such a long time as it currently stands.

const arr1 = [  
    {  
       "number":"123",
       "firstName":"John",
       "lastName":"Smith",
       "email":"test1@test.com",
    },
    {  
       "number":"1234",
       "firstName":"Chad",
       "lastName":"Baker",
       "email":"test2@test.com",
    }
];
 
const arr2 = [  
    {  
        "number":"12345",
        "firstName":"Chad",
        "lastName":"Baker",
        "email":"test2@test.com",
    },
    {  
        "number":"123456",
        "firstName":"John",
        "lastName":"Smith",
        "email":"test1@test.com",
    }
]

 let arr3 = arr1.filter(a => arr2.some(b => { return a.firstName == b.firstName && a.lastName == b.lastName}));  
 
 console.log(arr3);

>Solution :

Create an id based on your matching criterion and add it to a Set for one of your arrays. Then you have a constant lookup time

let createId = (x) => `F:${x.firstName};L:${x.lastName}`;

//ids is a Set<string> which will only contain values created by the createId function
let ids = new Set(arr2.map(x => createId(x)));

//now in the filter, you have a set-lookup which is O(1) instead of 
//a linear search which is O(n)
let intersection = arr1.filter(x => ids.has(createId(x)));

Of course, you can modify the createId however you want. By calling it also in the callback of filter, you make sure, all objects are treated the same.

Leave a ReplyCancel reply