Good evening,
The problem with the next piece of code is that I need to save the positions of all the repeated values that are found, for example:
Using the FIFO algorithm
marcos = [4,1,2]
cadena = [5,1,2,3,4,5]
The first one that enters is the first to go out therefore, and l 5 replaces the 4 , but when a value is repeated it should not be replaced since said value is already in the array marcos
.
Following the logic of the algorithm this should jump the repeated values that this case would be 1,2
and continue [5,3,4]
and therefore the 5 would not be replaced either.
Then in my array
of "repeated" should be saved [1,2]
which are the corresponding positions of the elements that are repeated in the array frames.
for(var i = 0; i < marcos.length; i++){
if(marcos[i] == cadena[i]){
var repetido = cadena.indexOf(cadena[i]);
array.push(repetido);
cadena.splice(repetido , 1);
}
if(array.length > 0){
//aca iria el
marcos.splice(array[i] , 1 , cadena[i])
//y luego se elimina el elemento del array para eguir con el proceso de forma normal
}else{
marcos.splice(i, 1, cadena[i]);
}
}
Making use of the optimal algorithm
The difference between this and the FIFO is that it replaces the farthest value.
marcos [1,2,3] cadena [5 , 4 , 3 , 2 , 1]
then the farthest value in this case would be one and you would have to replace 5 with 1 obtaining this result: [5,2,3]
cadena [4 , 3 , 2 , 1]
to then repeat the procedure, but in this case the value 5 will not return to then use the result would be: [4,2,3]
Could someone give me a hand with the logic of these problems?
Thank you very much.