I have been asked to write a function that finds the total number of duplicate elements in any array. If an element shows up more than twice, I'm not sure how to stop the counter from incrementing.
ex) if input = [1,2,1,4,2,6,1], my output should be = 2
function countDuplicates(arr) {
let counter = 0;
for (let outer = 0; outer < arr.length; outer++) {
for (let inner = arr[outer + 1]; inner < arr.length; inner++) {
if (arr[outer] === arr[inner]) {
counter++;
}
}
}
return counter;
}
console.log(countDuplicates([1,2,1,4,2,6,1]));
break;after thecounter++;