0

I am trying to see how many times a value in one array appears in another. This was one thing I but it didn't work.

arr1 = [1, 2, 3, 4, 5];

arr2 = [1, 7, 8, 9, 10];


count = 0;

for (x in arr2){
        for (y in arr1){
                if (x == y){
                        count +=1;
                }
        }
}

console.log(count);

Another thing i tried was this.

(arr1.some((val)=>{return arr2.includes(val);} ))

It checks if at least one value matches but i wasn't sure on how to implement a count for it.

My goal is to see how many times a value from arr2 appears in arr1. It should return 1 in this case.

J.J.Wood
  • 11
  • 4
  • 2
    `for ... in` loops over the property names (the array indexes). You want `for ... of`. – Pointy May 17 '22 at 16:27
  • Related sub-solutions at [How to count certain elements in array?](https://stackoverflow.com/questions/6120931/how-to-count-certain-elements-in-array) – jarmod May 17 '22 at 16:28
  • 1
    what result do you expect? – Nina Scholz May 17 '22 at 16:49
  • some is just going to tell you if one exists, it is not going to keep looking after it finds it. You would need to use something like reduce. – epascarello May 17 '22 at 16:58
  • The question is unclear. Do you want the total intersections or subtotals for each intersection? And does array 1 always have unique values? If not, how should duplicate values be counted? – Yogi May 17 '22 at 17:09

4 Answers4

2

You could use Array.prototype.reduce in combination with Array.prototype.filter in order to get an Object of repeated values

const arr1 = [1, 2, 3, 4, 5, 1, 1, 1, 9, 9]; // Repeated values
const arr2 = [1, 7, 8, 9, 10]; // Unique values

const appearances = (arrUnique, arrRepeated) => arrUnique.reduce((ob, valUnique) => {
  ob[valUnique] = arrRepeated.filter(v => valUnique === v).length;
  return ob;
}, {});

console.log(appearances(arr2, arr1));    // {value: counts, ...}
console.log(appearances(arr2, arr1)[1]); // 4

which will return:

{
  "1": 4,   // repeats 4 times
  "7": 0,
  "8": 0,
  "9": 2,   // repeats 2 times
  "10": 0
}
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
1

You could take an object from the counting values, then iterate the second array and count only wanted values.

const
    array1 = [1, 2, 3, 4, 5],
    array2 = [1, 7, 8, 9, 10],
    result = array1.reduce(
        (r, v) => (v in r && r[v]++, r),
        Object.fromEntries(array2.map(v => [v, 0]))
    );

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

x and y will be the indexes of the loops when using this syntax. So get what you are after you could do it like this.

    const arr1 = [1, 2, 3, 4, 5];

const arr2 = [1, 7, 8, 9, 10];


count = 0;

for (x in arr2){
        for (y in arr1){
                if (arr2[x] === arr1[y]){
                        count +=1;
                }
        }
}

console.log(count);

 
P-A
  • 1,161
  • 12
  • 16
0

Details are commented in the example and printed on the console. Using reduce() and Map() concept was from here.

// Utility function
const log = data => console.log(JSON.stringify(data));

log(`const arr1 = [1, 2, 3, 4, 5];`);
log(`const arr2 = [1, 7, 8, 9, 10];`);
log(`const arr3 = [1, 2, 3, 4, 5, 6, 7];`)
log(`const arr4 = [1, 7, 8, 9, 10];`);

const arr1 = [1, 2, 3, 4, 5];
const arr2 = [1, 7, 8, 9, 10];
const arr3 = [1, 2, 3, 4, 5, 6, 7];
const arr4 = [1, 7, 8, 9, 10];

const times = (arrA, arrB) => 
  // Returns n if...  
  arrB.filter(n => 
    // ...n exists in arrA
    arrA.includes(n))
    // Return how many matches
    .length;
    
log(`@param: arr1, arr2`)
log(times(arr1, arr2));
log(`@param: arr3, arr4`)
log(times(arr3, arr4));

// ...Rest operator will accept an unlimited amount
const frequency = (...arrays) => {
  // Combine params into one array
  let array = [...arrays.flat()],
  min;
  // If the first param is a string...
  if (''+array[0] === array[0]) {
    /*
    ...remove it from the array and convert it into a number.
    Otherwise it's 1
    */
    min = +array.shift();
  } else min = 1;
  
  // 4. Convert Map into an array of [key, value] pairs
  return  [...array
    .reduce((grp, val) => 
      /*
      3. Set [value] on Map to the it's current value +1
      */
      grp.set(val, 
        /*
        2. Get [value] from Map, but if it doesn't exist yet then it's a 
           zero.
        */
        (grp.get(val) || 0) + 1)
        // 1. Create Map object
        , new Map()
    )]
    // 5. Return each pair that meets or exceeds min
    .filter(([key, val]) => val >= min);
}

log(`@param: arr1, arr2`);
log(frequency(arr1, arr2));

log(`@param: arr3, arr4`);
log(frequency(arr3, arr4));

log(`frequency() will accept anything in unlimited amounts`);
log(`@param: arr1, arr2, arr3, arr4, 10, 8`);
log(frequency(arr1, arr2, arr3, arr4, 10, 8));

log(`Pass first parameter as a string of a number if you want a minimum count returned`);
log(`@param: '2', arr3, arr4`);
log(frequency('2', arr3, arr4));

log(`If you want use an object instead an array pairs, use Object.fromEntries(frequency(arr1, arr2))`);
log(Object.fromEntries(frequency(arr1, arr2)));
.as-console-row::after { width: 0; font-size: 0; }
.as-console-row-code { width: 100%; word-break: break-word; }
.as-console-wrapper { min-height: 100% !important; min-width: 100%; }
zer00ne
  • 41,936
  • 6
  • 41
  • 68