2

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.

4 Answers 4

4

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);

Sign up to request clarification or add additional context in comments.

2 Comments

is it really more effective?
I haven't profiled it, but the complexity is O(n + m) (n - basket1.length, m - basket2.length) vs. O(n * m) with forEach/filter and includes.
0

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)

2 Comments

What exactly is the benefit of replacing .forEach() with .filter()?
It is the same as using loops but this is much cleaner as mentioned in the answer
0

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

e.g.

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.