eliminate successive equal chains

1

My code eliminates the successive chains when there are 2 equal chains but when there is more, it does not eliminate.

Example with 2 successive chains equal "a", "a"

var string = ["a", "a", "b", "c", "d", "e", "f", "g"];
eliminarVecinosRepetidos(string);

function eliminarVecinosRepetidos(cadenas) {
  for (var x = 0; x < cadenas.length; x++) {
    for (var i = x + 1; i < cadenas.length; i++) {
      if (cadenas[i] === cadenas[x]) {
        cadenas.splice(i, 1);
      }
    }
  }
  console.log(cadenas);
  return cadenas;

}

Now with 3 "a","a","a"

var string = ["a", "a", "a", "b", "c", "d", "e", "f", "g"];
eliminarVecinosRepetidos(string);

function eliminarVecinosRepetidos(cadenas) {
  for (var x = 0; x < cadenas.length; x++) {
    for (var i = x + 1; i < cadenas.length; i++) {
      if (cadenas[i] === cadenas[x]) {
        cadenas.splice(i, 1);
      }
    }
  }
  console.log(cadenas);
  return cadenas;

}
    
asked by hubman 16.01.2018 в 21:01
source

4 answers

1

Using Array#reduce

Checking that the previous value is different from the current one.

We use the second argument of the reduce method ( valorInicial ), which will be a list that will be filled in each iteration with the values that satisfy that the previous element is different.

var array = ["a", "a", "a", "b", "c", "d", "e", "f", "g", "a"]

var filtrado = array.reduce((res, val, idx, arr) => 
  (val != arr[idx - 1] && res.push(val) && res) || res, [])

console.log(filtrado)

Using Array#filter

In a very similar but more eloquent way:

var array = ["a", "a", "a", "b", "c", "d", "e", "f", "g", "a"]

var filtrado = array.filter((el, idx, arr) => el != arr[idx - 1] )

console.log(filtrado)
    
answered by 17.01.2018 / 00:22
source
1

You have the problem in the second loop:

  • Find the first character a in index 0.
  • Run the array from index 1
  • Find a in index 1 and remove it
  • The array is ["a", "a", "b", "c", "d", "e", "f", "g"]
  • Check index 2 for a a (when you should recheck 1)

If you subtract one from the index after the splice you will get the desired result:

var string = ["a", "a", "a", "b", "c", "d", "e", "f", "g"];
eliminarVecinosRepetidos(string);

function eliminarVecinosRepetidos(cadenas) {
  for (var x = 0; x < cadenas.length; x++) {
    for (var i = x + 1; i < cadenas.length; i++) {
      if (cadenas[i] === cadenas[x]) {
        cadenas.splice(i, 1);
        i--;
      }
    }
  }
  console.log(cadenas);
  return cadenas;

}

Although in reality what the code does is to eliminate the characters from the next position until it is different or the end of the array is reached. So better than with the for you could do:

var string = ["a", "a", "a", "b", "c", "d", "e", "f", "g"];
eliminarVecinosRepetidos(string);

function eliminarVecinosRepetidos(cadenas) {
  for (var x = 0; x < cadenas.length; x++) {
    while (x + 1 < cadenas.length && cadenas[x] === cadenas[x+1]){
      cadenas.splice(x + 1, 1);
    }
  }
  console.log(cadenas);
  return cadenas;

}

Another more compact option: go through the array once from the second position, eliminating the character if it is the same as the previous one:

var string = ["a", "a", "a", "b", "c", "d", "e", "f", "g"];
eliminarVecinosRepetidos(string);

function eliminarVecinosRepetidos(cadenas) {
  for (var i=1; i<cadenas.length; i++){
    if (cadenas[i] === cadenas[i-1]){
      cadenas.splice(i--,1);      
    }    
  }
  console.log(cadenas);
  return cadenas;
}
    
answered by 16.01.2018 в 21:10
1

What you can do is verify that the last element of the array that you saved the elements, is not equal to the current element of the array that you pass as a parameter:

var data = ["a", "a", "a", "b", "c", "c", "d", "e", "f", "g", "a"];

function eliminarVecinosRepetidos(data){
   var result = [];
   for(var i=0;i<data.length;i++) {
        if(result.length == 0 || result[result.length-1] != data[i]){
          result.push(data[i]);
        }
   }
   
   return result;
}

console.log(eliminarVecinosRepetidos(data));

result.length == 0 verifies that the if is the first element, we always add it.

result[result.length-1] != data[i] what it does is verify that the last element is not equal to the current element of the array being processed.

    
answered by 16.01.2018 в 21:10
1

You could solve it like this:

  • We use array.forEach , to iterate the fix.
  • Initially we compared the first cadena against empty, since it is the first
  • If the cadena is different from the controlled one, then we add it to the response array and set it as the new controlled string.

Demo:

console.log(eliminarVecinosRepetidos(["a", "a", "b", "c", "d", "e", "f", "g", "a"]));

function eliminarVecinosRepetidos(words) {
  let result = [],
    check = '';
    
  words.forEach(function (word) {
    if (word == check) return;
    result.push(check = word);
  });
  return result;
}

Update

You could also use array.filter

Demo:

console.log(eliminarVecinosRepetidos(["a", "a", "b", "c", "d", "e", "f", "g", "a"]));

function eliminarVecinosRepetidos(words) {
  let check = '';
  return words.filter(function(word) {
    return word != check ? check = word : false;
  });
}
    
answered by 16.01.2018 в 21:26