I am trying to exclude duplicate numbers from the array. For example: if I filter an array of [1,2,2,3,5,5] the output should be [1,3].
function unique(arr) {
let sortArr = arr.sort()
let res = []
for (let i = 0; i < arr.length; i++) {
if (sortArr[i] != sortArr[i + 1]) {
res.push(sortArr[i])
}
}
return res
}
console.log(unique([1, 2, 2, 3, 5, 5]))
I was trying not to add duplicate numbers into an array, but instead of [1,3] I am getting [1,2,3,5]