0

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!

AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
Aiden
  • 59
  • 4
  • 2
    Does this answer your question? [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – ponury-kostek Jul 27 '21 at 18:18
  • Why would it be only three times? The code nests entire-array iterations, so `i[0] === j[4]` *and* `j[5]`. When you get to `i[4]` it'll equal `j[4]` and `j[5]` again, and so on. FWIW this is a good exercise to "play computer" with pencil and paper on, or step through the code, or put in some console logging to understand what's happening. – Dave Newton Jul 27 '21 at 18:20
  • try `let j = i + 1` – James Jul 27 '21 at 18:21
  • There is no point in providing a default value of `undefined` to the `arr` argument of the function. First of all, without a default value `arr` is `undefined` by default if the function is invoked without arguments (i.e. `removeDuplicates()`). Then, if `arr` is `undefined` then the code throws an error when it reaches `arr.length`. If you want to have a default value for `arr` (even if it does not make any sense for this function) then use an empty array (`(arr = []) => { ... }`). – axiac Jul 27 '21 at 18:26

2 Answers2

1

Despite this solution being not the most effective one, the easiest way to fix the code would be

let count = 0;
for (let i = 0; i < arr.length - 1; i++){
    for (let j = i + 1; j < arr.length; j++){
        if (arr[i] === arr[j]){
            count++;
       }
    }
}

Your original code finds too many duplicates because despite pairs (1, 3) and (3, 1) being identical, your code treats them as different.

saferif
  • 187
  • 10
  • (Noting that this would return `2`, I think, but it's not clear if the OP is trying to count all instances of a duplicated entry, or count the number of duplicates of an entry.) – Dave Newton Jul 27 '21 at 18:26
  • With this validation, I wanted to count how many times one number gets repeated on an array – Aiden Jul 27 '21 at 19:07
0

The problem is logic. You are executing the first cycle 6 times, while the second cycle is executed 5 times for each iteration of the previous one. There will be cycles that will enter the if in more than one opportunity try the following:

 const removeDuplicateds = (arr=undefined)=>{
    let result = arr.filter((item,index)=>{
      return arr.indexOf(item) === index;
    })
    return result;
 }
Lance Kind
  • 949
  • 12
  • 32