i'm learning javascript and trying to solve some exercises. The one I'm into wants me to remove all the duplicates from an array. By far this is my code:
const removeDuplicateds = (arr=undefined)=>{
let count=0;
for (let i=0; i <arr.length ; i++){
for (let j=1; j<arr.length; j++){
if (arr[i]=== arr[j]){
count= count +1;
}
}
}
return console.log(count);
removeDuplicateds([1,2,3,4,1,1]);
So I wanted to validate this structure by using a counter (the real practice would be to use an arr.splice(j,1)
instead of the count=count+1
). So it returns 12 times, which means it entered 12 times in the if condition, which is impossible because there should be only 3 times (three 1's in the arr). Any ideas of what's happening?
thanks!