Created
January 25, 2021 02:48
-
-
Save KakoozaJerry/6abc40f606e7c98a8bdd3247371d32df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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