I'm attempting to develop a function that identifies duplicate numbers within an array and collects these duplicate numbers into a new array.
Here's the code I've come up with so far, but I'm encountering some challenges.
function findDuplicates(array) {
var duplicates = [];
for(var i = 0; i < array.length; i++) {
if (array[i] === 1) {
duplicates.push(array[i]);
}
}
return duplicates;
}
alert(findDuplicates([1, 2, 3, 4, 5, 1]));
findDuplicates([2, 1, 1, 2, 2]); //Expected output: [1, 2, 2]. The original documents are 2, 1 and the copies are 1, 2, 2