Reputation: 195
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));
Upvotes: 1
Views: 1232
Reputation: 17190
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`);
});
Upvotes: 1
Reputation: 33726
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);
Upvotes: 1
Reputation: 13963
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));
Upvotes: 0
Reputation: 386604
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]));
Upvotes: 0
Reputation: 7851
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
}
Upvotes: 0
Reputation: 5432
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));
Upvotes: 3
Reputation: 104775
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
Upvotes: 1
Reputation: 6872
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));
Upvotes: 0