Skip to content

Instantly share code, notes, and snippets.

@KakoozaJerry
Created January 25, 2021 02:48
Show Gist options
  • Save KakoozaJerry/6abc40f606e7c98a8bdd3247371d32df to your computer and use it in GitHub Desktop.
Save KakoozaJerry/6abc40f606e7c98a8bdd3247371d32df to your computer and use it in GitHub Desktop.
function countDuplicates(numbers){
//[1,1,1,2,3,3,4,5,6]
//[1,3,1,4,5,1,6,3,2]
numbers.sort((a,b) => a - b)
// sort the array
let count = 0
let left = 0
let right = 1
//loop through numbers
while (right < numbers.length){
if(numbers[left] == numbers[right]){
count++
while(numbers[left] == numbers[right]){
right++
}
left = right
right++
}else{
left = right
right++
}
}
return count
}
console.log(countDuplicates([1,3,1,4,5,1,6,3,2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment