function duplicates(arr) { let duplicateObj = new Set() let result = [] for(let i=0;i<arr.length;i++){ console.log("current element",arr[i],"has is",duplicateObj.has(arr[i])) if(duplicateObj.has(arr[i])){ result.push(arr[i]) }else{ duplicateObj.add(arr[i]) } } let resultSet = new Set(result) return [...resultSet] } console.log(duplicates([1, 2, 4, 4, 3, 3, 1, 5, 3]))