Task: I have to check if there are any two values consecutively same in an array. If it does. I have to take that repeated value into a different place to not have 2 identical values next to each other.
Problem: It returns there are no identical values, when definitely there are more than one. I know that this is happening because the "if" compares the first two elements and as they are not the same returns and stop the loop. But what I need is to complete the whole loop and if there are consecutive repeated values enter into the next loop.
I've tried to figure it out solution and genuinely understand it for a long time, and I couldn't.
What I've tried: (I am working with a Stack, I will not post the methods of it because I know they are working fine for other exercises)
function noIdenticalConsecutives(arr) {
let stack = new Stack();
let repeat = [];
if (arr.length === 0) return "No values to iterate";
for (let i = 0; i < arr.length - 1; i ) {
const curr = arr[i];
const next = arr[i 1];
if (curr !== next) return "There are no identical consecutive values";
}
for (let i = 0; i < arr.length; i ) {
if(arr[i] === arr[i 1]){
repeat.push(arr[i])
}else{
stack.push(arr[i])
}
}
for (let i = 0; i < repeat.length; i ) {
const element = repeat[i];
stack.push(element);
}
return stack;
}
noIdenticalConsecutives([14, 4, 10, 7, 3, 1, 1, 5, 7, 7]);//There are no identical values
CodePudding user response:
The problem here is that you have prematurely returned. This should fix the problem
function noIdenticalConsecutives(arr) {
let stack = new Stack();
let repeat = [];
let hasRepeat = false;
if (arr.length === 0) return "No values to iterate";
for (let i = 0; i < arr.length - 1; i ) {
const curr = arr[i];
const next = arr[i 1];
if (curr === next) hasRepeat = true;
}
if (!hasRepeat) return "There are no identical consecutive values";
}
CodePudding user response:
I thing you can replace all the code above using one single array.reduce like that:
const stackAndRepeat = arr.reduce((accumulator, next) => {
accumulator.stack[accumulator.stack.length - 1] === next ? accumulator.repeat.push(next) : accumulator.stack.push(next);
return accumulator;
}, { stack: [], repeat: [] });
Now stackAndRepeat.stack and stackAndRepeat.next should contain the correct values for you.
CodePudding user response:
Your algorithm is wrong. in the First case when (14 != 4) this will return no identical values. hence your code will stop on the first case and didn't check further.
You can correct your algo. by modifying this-
let isIdentical = false;
for (let i = 0; i < arr.length - 1; i ) {
const curr = arr[i];
const next = arr[i 1];
if (curr == next) { isIdentical = true; return; }
}
if(!isIdentical){ return "There are no identical consecutive values";}
CodePudding user response:
Ok, first you have to separate out the repeated elements into another array then concat it with the filtered array.
function noIdenticalConsecutives(arr) {
let stack = [];
let repeat = [];
for (let i = 0; i < arr.length; i ) {
if (arr[i 1] !== arr [i]) {
stack.push(arr[i]);
} else {
repeat.push(arr[i]);
};
};
return stack.concat(repeat);
};
console.log(noIdenticalConsecutives([14, 4, 10, 7, 3, 1, 1, 5, 7, 7]));
// [14, 4, 10, 7, 3, 1, 5, 7, 1, 7]