0

I have an array with some strings and would like to loop through the array to check that i dont have two of the same string.

In java/javascript i would use the equals() method like below, but that dont seem to work.

array: Array<any> = [];
for(var i = 1; i < array.length; i++){
  if(array[i].equals(array[i-1])){
    array.splice(i);
  }
}

Is there any other function or easy way to do this?

asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84
piiiias
  • 15
  • 1

3 Answers3

0

You need to use the == operator or the === operator (more info about the difference here: Which equals operator (== vs ===) should be used in JavaScript comparisons).

So:

for(var i = 1; i < array.length; i++){
  if(array[i] === array[i-1]){
    array.splice(i);
  }
}

But you need to array to be sorted for that to work.
Here's an altenative implementation:

let array = ["one", "two", "one", "three", "two", "four", "one"];

array.sort((a, b) => {
    if (a < b) return -1;
    if (a > b) return 1;
    return 0;
}).filter((item, index) => item !== array[index - 1]); // ["four", "one", "three", "two"]

(code in playground)


Edit

If your array contains dates then you should do:

for(var i = 1; i < array.length; i++){
  if(array[i] == array[i-1]){
    array.splice(i);
  }
}

Or

for(var i = 1; i < array.length; i++){
  if(array[i].toString() === array[i-1].toString()){
    array.splice(i);
  }
}
Community
  • 1
  • 1
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • Yeah, maybe I should've mentioned it, but my array is containing a lot of "date strings", meaning they are of type string but they are dates, and they are sorted. My problem is that I have more than one of each date some places, and when i loop through it and try to remove all those which are duplicates of the one before them something happens so that im removing all my strings except the first one... "===" dont seem to work either but maybe the mistake is elsewhere if that is supposed to work like the equals method? – piiiias Oct 18 '16 at 10:35
  • Check my revised answer – Nitzan Tomer Oct 18 '16 at 10:38
  • hey, i now found out i have to add a number for how many items it should remove from my array in the splice method. So the splice line should be like this: array.splice(i, 1); thats what made it remove all my strings... :P – piiiias Oct 18 '16 at 17:57
0

I'm afraid the algorithm you propose won't work unless the array of strings is in sorted order, so duplicate entries are next to each other.

If the size of the array is small-ish, a very straight-forward solution is to loop over the array twice, like this:

for (var i = 0; i < array.length; i++)
  for(var j = i+1; j < array.length; j++)
    if (array[i] == array[j])
      array.splice(j);

Other classic methods include sorting the array beforehand and then doing what you are doing, or employing an object / hashset to filter elements.

Horia Coman
  • 8,681
  • 2
  • 23
  • 25
0

Here is a version using reduce & Object.key's.

It should be pretty good on performance too, as it using object literal as a kind lookup.

let array = ["one", "two", "one", "three", "two", "four", "one"];

let deduped = Object.keys(array.reduce((c,a) => { c[a]=true; return c; },{}))

console.log(deduped);
Keith
  • 22,005
  • 2
  • 27
  • 44