简体   繁体   中英

Javascript Array - checking for matching item across 2 arrays

I have 2 arrays of string objects.

let count = 0;

var basket1 = [‘Apples’,  ‘Cucumber’, ‘Lettuce’, ‘Bananas’, ‘Pears’, ‘Cauliflower’, ‘Strawberry’]

var basket2 = [‘Apples’, ‘Bananas’, ‘Oranges’, ‘Pears’, ‘Pineapple’, ‘Strawberry’]

When there is an item in a basket1 and basket2 that is the same, I want to increment count.

Note: the length of basket1 and basket2 can vary such that basket1.length > basket2.length, basket1.length < basket2.length or basket1.length = basket2.length,

I was thinking:

  1. Loop through basket with basket.forEach( (item) => {} )
  2. forEach item, if otherBasket.includes(item), count++

I was just wondering if there was a more efficient way of doing this.

If the each item can appear only once in each array, you can combine them to a single array, and then the count would be the total length of the combine array - the size of the Set of the combined arrays (only unique items).

 const basket1 = ['Apples', 'Cucumber', 'Lettuce', 'Bananas', 'Pears', 'Cauliflower', 'Strawberry'] const basket2 = ['Apples', 'Bananas', 'Oranges', 'Pears', 'Pineapple', 'Strawberry'] const combined = [...basket1, ...basket2]; const count = combined.length - new Set(combined).size; console.log(count);

If the item can appear multiple times in each array:

 const basket1 = ['Apples', 'Cucumber', 'Lettuce', 'Bananas', 'Bananas', 'Pears', 'Cauliflower', 'Strawberry', 'Apples']; // apple appears twice, bananas appears twice const basket2 = ['Apples', 'Bananas', 'Bananas', 'Oranges', 'Pears', 'Pineapple', 'Strawberry']; // apple appears once, bananas appear twice // create an object with the counts of each item in basket1 const total1 = basket1.reduce((r, s) => { r[s] = (r[s] || 0) + 1; return r; }, {}); const count = basket2.reduce((cnt, s) => { // if has a value > 0 in total1, decrment the value, and add 1 to the count if(total1[s]) { total1[s]--; cnt++; } return cnt; }, 0); console.log(count);

Yes, you can use Array.prototype.filter() to check if an item from array A is present in the array B .

It is the same as using loops but this is much cleaner

 let count = 0; var basket1 = ['Apples', 'Cucumber', 'Lettuce', 'Bananas', 'Pears', 'Cauliflower', 'Strawberry'] var basket2 = ['Apples', 'Bananas', 'Oranges', 'Pears', 'Pineapple', 'Strawberry'] basket1 = basket1.filter(item => basket2.includes(item)); count = basket1.length console.log(basket1,count)

You could create a combined hash map using both arrays. Then finding the count is summing the values of the combined hash map.

eg

const arr1 = ['a', 'b', 'c'];
const arr2 = ['b', 'c', 'd'];
  1. Loop through 1st array and create a map which looks like this:

hashMap = {'a': 1, 'b': 1, 'c': 1};

  1. Loop through 2nd array and update your hash map, if key exists, increment the value else add a new entry. Then the hashMap looks like this:

hashMap = {'a': 1, 'b': 2, 'c': 2, d: 1};

  1. Do an Object.values(hashMap) , which gives array [1,2,2,1]

  2. Sum these values.

With this approach, you could solve it with O(n) time with some space complexity of O(n1 + n2) where n1 and n2 are number of items in both arrays.

It depends what mean "efficient" in your case. If you are talking about the performance, you could keep your array sorted. In this case you can go throw both of arrays in single cycle. And probably it will be faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM