1var removeDuplicates = function (s, k) {2const stack = [];3for (const c of s) {4const obj = { count: 1, char: c };5if (!stack.length) {6stack.push(obj);7continue;8}9const top = stack[stack.length - 1];10if (top.char === obj.char && obj.count + top.count === k) {11let count = k;12while (count > 1) {13stack.pop();14count--;15}16} else if (top.char === obj.char) {17obj.count += top.count;18stack.push(obj);19} else {20stack.push(obj);21}22}23return stack.reduce((a, b) => a + b.char, "");24};