0

I wrote a function in javaScript that checks if it is the first time the numbers has been seem, if so, it will be added to the the array. For some reason the first number is always repeating it, Ex:

if we pass [1,1,1,2,3,3,4,5] it will print out [1,1,2,3,4,5] instead of [1,2,3,4,5]

Could anyone tell me what I'm missing ?? Thank you in advance.

var numbers = [1,1,1,1,1,2,3,4,4,4,5,5,6,6,6,8];
function eliminateDup(arr){

    var myArr = [];

    for(var i = 0;i<arr.length;i++){

        if(!myArr[arr[i]]){

            myArr.push(arr[i]);
        }
    }
    return myArr;
}
console.log(eliminateDup(numbers));
Dij
  • 9,761
  • 4
  • 18
  • 35
cubila
  • 43
  • 2
  • 8
  • 1
    why wouldn't you just use `indexOf` instead of this mess: `!myArr[arr[i]]` – Derek Pollard Aug 06 '17 at 19:15
  • 3
    Note that you can do all that with `uniques = [...new Set(numbers)]` – trincot Aug 06 '17 at 19:16
  • This assumes the input is sorted ascending – ASDFGerte Aug 06 '17 at 19:16
  • @ASDFGerte, neither is true. – trincot Aug 06 '17 at 19:18
  • OK, we have four (oh wait, we've got five) answers now suggesting `indexOf`. Maybe someone can be a bit more original? – trincot Aug 06 '17 at 19:19
  • @trincot why dont you explain your first comment as an answer – Derek Pollard Aug 06 '17 at 19:21
  • Because it really is a duplicate. – trincot Aug 06 '17 at 19:23
  • @trincot elaborate please, i had to think about the ascending part because it wasn't enough, it needs to be ascending and not omitting any numbers. The zero based part was also not enough, the offset is `arr[0]` as in `if(!myArr[arr[i] - arr[0]]){` but won't work for anything greater than 1. Explicitly, if you push an eight to the numbers in the posted example, it will also get doubled in the result because the seven was omitted. – ASDFGerte Aug 06 '17 at 19:29
  • The whole idea of `!myArr[arr[i]]` is wrong for one reason: it uses array *values* as *indexes*, while the two are completely different concepts and have nothing to do with eachother. This has nothing to do with sort-order or zero-based. Imagine an input array with `[500, 600, 700]`. – trincot Aug 06 '17 at 19:34

3 Answers3

1

This comparison doesn't make sense :

if(!myArr[arr[i]]){

It is true only if !myArr[arr[i]] can be converted to true.
You get twice 1 in the target array myArr because both arr[0] and arr[1] equals to 1 and myArr has an undefined element at the 1 index for the two first iterations at the time where the conditional statement is executed.


First iteration

i = 0 
arr[0] = 1
myArr[1] = undefined

So if(!myArr[1]) is true.
You add 1.

Second iteration

i = 1     
arr[1] = 1
myArr[0] = 1 // now valued but not used in the test
myArr[1] = undefined;

So if(!myArr[1]) is true.
You still add 1.

Third iteration

i = 2
arr[2] = 1
myArr[0] = 1 
myArr[1] = 1

So if(!myArr[1]) is false.
nothing is added.


You should rather check whether the current value is not contained in the target array before adding it in the target array :

if(myArr.indexOf(arr[i])==-1){
    myArr.push(arr[i]);
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

use this condition instead, for push

if(myArr.indexOf([arr[i])===-1)
Koushik Chatterjee
  • 4,106
  • 3
  • 18
  • 32
  • thank you so much guys, as a beginner I'm struggling sometimes to debug my own codes. it mades complete sense what you all have explained to me. Thanks again. – cubila Aug 07 '17 at 13:51
0

myArr.push(arr[i]); will push to back of the array myArr (just like a stack) and thus element '1' will be at index 0.

myArr[arr[i]] checks if element at index arr[i] exists which it won't so will push another 1.

As suggested above use indexof() which will just search and give an index if the element exists.