Cho mảng, trả về mảng mới chỉ chứa các phần tử xuất hiện nhiều hơn một lần (mỗi phần tử trùng chỉ lấy một lần).
Ví dụ
const arr = [3, 6, 7, 5, 3]; const makeDuplicatesArray = (arr = []) => { const res = []; for(let i = 0; i < arr.length; i++){ if(i === arr.lastIndexOf(arr[i])){ continue; }; res.push(arr[i]) }; return res; }; console.log(makeDuplicatesArray(arr));
Kết quả
[3]