How to remove all adjacent duplicate characters from a string until no more can be removed in JavaScript

1 Answer

0 votes
function removeAdjacentDuplicates(s) {
    const stack = [];

    for (const ch of s) {
        if (stack.length > 0 && stack[stack.length - 1] === ch) {
            stack.pop();        // pop
        } else {
            stack.push(ch);     // push
        }
    }

    return stack.join('');
}

const s = "abbacccada";

console.log(removeAdjacentDuplicates(s));   



/*
run:

cada

*/

 



answered Mar 7 by avibootz
edited Mar 7 by avibootz

Related questions

...