Level
LeetCode Easy
Recruitment
var removeDuplicates = function (S) {
  const stack = [];
  let lastIndex = -1;

  for (const char of S) {
    if (stack[lastIndex] === char) {
      stack.pop();
      lastIndex--;
    } else {
      // empty or different char
      stack.push(char);
      lastIndex++;
    }
  }

  return stack.join("");
};