Home > database >  Check how many times a value in an array appears in another array
Check how many times a value in an array appears in another array

Time:05-18

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.

CodePudding user response:

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
}

CodePudding user response:

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

CodePudding user response:

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

 

CodePudding user response:

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%; }

  • Related