[JavaScript] Easy to understand - 3 solutions
Solution 1\n\nSince we need to check and remove the previous char by the current char, so it let us think about the stack:\n1. traverse the string s\n2. for eve
## Solution 1\n\nSince we need to check and remove the previous char by the current char, so it let us think about the stack:\n1. traverse the string `s`\n2. for every char, we compare it with the top value in stack\n\t- if they are same, pop the top value\n\t- otherwise, push the current char into stack\n3. finally, return the stack as a string\n\nHere\'s a sample code from me:\n\n```js\nconst removeDuplicates = s => {\n  const stack = [];\n  for (const char of s) {\n    stack[stack.length - 1] === char ? stack.pop() : stack.push(char);\n  }\n  return stack.join(\'\');\n};\n```\n\n## Solution 2\n\nFor this solution, we use 2 pointers. One for traversal and one for store result:\n1. the traversal pointer just traverse the string `s`\n2. the store pointer try to save char into the arr itself just as the `stack` in solution 1\n3. we compare every new char with the store pointer value and maintain the value of store pointer\n\nIn fact, this is just a variant of solution 1.\nHere\'s a sample code from me:\n\n```js\nconst removeDuplicates = s => {\n  const arr = s.split(\'\');\n  let ans = 0;\n  for (let i = 0; i < arr.length; ++i) {\n    ans === 0 || arr[i] !== arr[ans - 1] ? (arr[ans++] = arr[i]) : --ans;\n  }\n  return arr.slice(0, ans).join(\'\');\n};\n```\n\n## Solution 3\n\nFor this solution, we try to deal with all the adjacent duplicates right now, and loop again until there are no more adjacent duplicates.\nWe also use the RegExp to make the code easy.\n\nHere\'s a sample code from me:\n\n```js\nconst removeDuplicates = s => {\n  const s2 = s.replace(/(.)\\1/g, \'\');\n  return s2.length === s.length ? s : removeDuplicates(s2);\n};\n```
['JavaScript']