I am struggling to achieve the result [1,1,1,1,2,2,20,20] from the given array.
My goal is to extract all duplicate values into a new array, but I just can't seem to get it right. Can you please assist me?
const array = [1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20];
const dupArray = (arr) => {
let newArray = array.sort();
let filteredArray = [];
for (y = 0; y < newArray.length; y++) {
for (i = y + 1; i < newArray.length; i++) {
if (newArray[y] === newArray[i]) {
filteredArray.push(newArray[i]);
}
}
}
return filteredArray
};
console.log(dupArray());