1

I am trying to print the total number of duplicates in the array. Instead the total number of elements is printing from the array instead of the duplicates that are there

const ar = [10, 20, 20, 10, 10, 30, 50, 10, 20];

function pairs(a) {

  var pairCount = 0;

  for (var i = 0; i < ar.length; i++) {
    for (var j = i + 1; j < ar.length; j++) {
      if (ar[i] === ar[j]) {
        pairCount++;
      }

    }
  }
  return pairCount;
}
console.log(pairs(ar));

4
  • What is the expected result? Commented Feb 12, 2019 at 21:59
  • You want to count the pairs or the number of duplicates ? I see 4 x 10, 3 x 20, so 7 duplicates, is that what you want ? or the number of pairs with identical elements ? Commented Feb 12, 2019 at 21:59
  • Could you specify what answer and in what form do you expect in your example? What type of solution do you want an optimized vanilla one or lodash will do? Commented Feb 12, 2019 at 22:04
  • 1
    btw, you never use a. Commented Feb 12, 2019 at 22:09

8 Answers 8

3

If you want a total number of just the duplicates, meaning [1,1,2,2,3] // 2, then the solution can be pretty short and sweet with es6

const ar = [10, 20, 20, 10, 10, 30, 50, 10, 20];

const duplicateCount = (a) => a.length - new Set(a).size;

console.log(duplicateCount(ar));

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

Comments

1

If you want to just get the total count of duplicates, you can create another array with them filtered out, then just get the difference:

const ar = [10, 20, 20, 10, 10, 30, 50, 10, 20];
const duplicatesFilteredOut = ar.filter((a, i, self) => i === self.indexOf(a))
const totalDuplicates = ar.length - duplicatesFilteredOut.length

Comments

1

An alternative is using the function reduce with a special accumulator which provides a dupes property as the counter and a dict as a breadcrumb of previously counted numbers.

const ar = [10, 20, 20, 10, 10, 30, 50, 10, 20],
      dupes = ar.reduce((a, c) => 
                  (a.dupes += a.dict[c] || 0, a.dict[c] = 1, a), {dupes: 0, dict: {}}).dupes;
      
console.log(dupes);

Comments

1

If you want to count the number of values that have duplicated you can use filter() this way:

const ar = [10, 20, 20, 10, 10, 30, 50, 10, 20];

let res = ar.filter((x, i) => ar.some((y, j) => y === x && i !== j));
console.log(res.length);

If you need detailed information about duplicated values, then you can do something like this:

const ar = [10, 20, 20, 10, 10, 30, 50, 10, 20];

let res = ar.reduce((data, curr) =>
{
    data[curr] = data[curr] ? ++data[curr] : 1;
    return data;
}, {});

Object.entries(res).forEach(([val, numTimes]) =>
{
    if (numTimes > 1)
        console.log(`Value ${val} appears ${numTimes} times`);
});

1 Comment

The function filter is the worst approach because a new array is created only for getting the length.
0

You could use an object to keept track of each kind of element. Like this:

const ar = [10, 20, 20, 10, 10, 30, 50, 10, 20];

function pairs(arr) {

  const pairCount = {};
  
  for (let a of ar) {
    if (a in pairCount) {
      pairCount[a]++;
    } else {
      pairCount[a] = 1;
    }
  }
  
  return pairCount;
}
console.log(pairs(ar));

Comments

0

You can use this function instead to get the total number of duplicates in an array.

const getDupCount = array => {
    const uniqueItems = new Set(array)
    const duplicates = array.length - uniqueItems.size

    return duplicates
}

Comments

0

You could take a Set for already counted values.

function pairs(array) {
    var pairCount = 0,
        visited = new Set;

    for (var i = 0; i < array.length; i++) {
        if (visited.has(array[i])) pairCount++;
        visited.add(array[i]);
    }
    return pairCount;
}

console.log(pairs([10, 20, 20, 10, 10, 30, 50, 10, 20]));

Comments

0

Here is a way to return the duplicates by value in an object:

const values = [10, 20, 20, 10, 10, 30, 50, 10, 20];

function duplicates(arr) {
  return Object.values(arr).reduce((acc, val) => {
    acc.count[val] = (acc.count[val] || 0) + 1;
    if (acc.count[val] > 1) acc.duplicates[val] = acc.count[val];
    return acc;
  }, { count: {}, duplicates: {} }).duplicates;
}

console.log(duplicates(values));

console.log(Object.values(duplicates(values)).reduce((sum, x) => sum + x, 0));

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.