1

I am trying this solution but it is not giving me the result I want. I don't want to count the number of pairs of 1 cause it is not really a pair as it appears 4 times. I need to count the "perfect" pairs, like in this case: 5 and 2. Right now this is giving me 4 as result and it should be 2.

How could I achieve that? I am stuck.

let ar1 = [12, 5, 5, 2, 1, 1, 1, 1, 2];

const countPairs = (ar) => {
  let obj = {};

  ar.forEach((item) => {
    obj[item] = obj[item] ? obj[item] + 1 : 1;
  });

  return Object.values(obj).reduce((acc, curr) => {
    acc += Math.floor(curr / 2);
    return acc;
  }, 0);
};

console.log( countPairs(ar1) )

5
  • do you understand your own code? your saving all the matches to an object, why dont you just count the values which has 2 Commented Nov 11, 2021 at 3:15
  • not sure I get you, how would you do it? Commented Nov 11, 2021 at 3:18
  • shouldn't 1 be a true pair also? Commented Nov 11, 2021 at 3:19
  • look at Sopheak, that pretty much does what I was refering to Commented Nov 11, 2021 at 3:20
  • @decpk cause it appears 4 times, I only want perfect pairs, numbers that appear ONLY twice, not more, not less. Commented Nov 11, 2021 at 3:21

3 Answers 3

3

You can filter the object values by 2 and count the list

let ar1 = [12, 5, 5, 2, 1, 1, 1, 1, 2];

const countPairs = (ar) => {
  let obj = {};

  ar.forEach((item) => {
    obj[item] = obj[item] ? obj[item] + 1 : 1;
  });
  
  return Object.values(obj).filter(e => e == 2).length;
};

console.log(countPairs(ar1))

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

Comments

1

This can be one-liner using Map as:

const countPairs(arr)  => [...arr.reduce((dict, n) => dict.set(n, (dict.get(n) ?? 0) + 1), new Map()).values(),].filter((n) => n === 2).length;

let ar1 = [12, 5, 5, 2, 1, 1, 1, 1, 2];

const countPairs = (arr) =>
  [
    ...arr
      .reduce((dict, n) => dict.set(n, (dict.get(n) ?? 0) + 1), new Map())
      .values(),
  ].filter((n) => n === 2).length;

console.log(countPairs(ar1));

Comments

0

or that

const 
  ar1 = [12, 5, 5, 2, 1, 1, 1, 1, 2]
, countPerfectPairs = arr => arr.reduce((r,val,i,{[i+1]:next})=>
    {
    if(!r.counts[val])
      { 
      r.counts[val] = arr.filter(x=>x===val).length
      if (r.counts[val]===2) r.pairs++
      }
    return  next ? r : r.pairs
    },{counts:{},pairs:0}) 


console.log( countPerfectPairs(ar1) )

If you prefer Details:

const 
  ar1 = [12, 5, 5, 2, 1, 1, 1, 1, 2]
, countPerfectPairs = arr => arr.reduce((r,val)=>
    {
    if(!r.counts[val])
      { 
      r.counts[val] = arr.filter(x=>x===val).length
      if (r.counts[val]===2) r.pairs++
      }
    return r
    },{counts:{},pairs:0}) 


console.log( countPerfectPairs(ar1) )

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.