Simple Solution with Diagrams in Video - JavaScript, C++, Java, Python
Video\nPlease upvote here so others save time too!\n\nLike the video on YouTube if you found it useful\n\nClick here to subscribe on YouTube:\nhttps://www.youtu
# Video\nPlease upvote here so others save time too!\n\nLike the video on YouTube if you found it useful\n\nClick here to subscribe on YouTube:\nhttps://www.youtube.com/@mayowadan?sub_confirmation=1\n\nThanks!\n\nhttps://youtu.be/4Csw0XRW2Iw?si=AB-rCrYoQz2ZSAGs\n\n\n```Javascript []\n/**\n * @param {string} s\n * @return {string}\n */\nvar removeDuplicates = function (s) {\n    // Create an empty stack.\n    let stack = [];\n\n    // Iterare over the string\n    for (let c of s) {\n        // If stack has at least one character and\n        // stack\'s top character is same as the string\'s character.\n        if (stack.length > 0 && stack[stack.length - 1] === c) {\n            // Pop a character from the stack.\n            stack.pop();\n        } else {\n            // Otherwise, push that character onto the stack.\n            stack.push(c);\n        }\n    }\n\n    // Return the result string.\n    return stack.join("");\n};\n```\n```Python []\nclass Solution:\n    def removeDuplicates(self, s: str) -> str:\n        # Create an empty stack.\n        stack = []\n        # Iterate over the string\n        for char in s:\n            # If stack has at least one character and\n            # stack\'s top character is same as the string\'s character\n            if stack and stack[-1] == char:\n                # Pop a character from the stack.\n                stack.pop()\n            else:\n                # Otherwise, push that character onto the stack.\n                stack.append(char)\n\n        # Form a string from stack\'s elements and return that.\n        return "".join(stack)\n\n```\n```Java []\nclass Solution {\n    public String removeDuplicates(String s) {\n             // Create an empty stack.\n        Stack <Character> stack = new Stack<>();\n\n        // Iterare over the string\n        for (char c: s.toCharArray()) {\n            // If stack has at least one character and\n            // stack\'s top character is same as the string\'s character.\n            if (!stack.isEmpty() && stack.peek() == c) {\n                // Pop a character from the stack.\n                stack.pop();\n            } else {\n                // Otherwise, push that character onto the stack.\n                stack.push(c);\n            }\n        }\n\n        // Initialize a StringBuilder to construct the result string\n        StringBuilder result = new StringBuilder();\n        // Pop all characters from the stack and append them to the \n        // StringBuilder\n        while (!stack.isEmpty()) {\n            result.append(stack.pop());\n        }\n\n        // Since the characters were added in reverse order, \n        // reverse the StringBuilder to get the final result\n        return result.reverse().toString();\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    string removeDuplicates(string s) {\n        // Create an empty stack.\n        stack<char> stack;\n\n        // Iterate over the string\n        for (char c : s) {\n            // If stack has at least one character and\n            // stack\'s top character is the same as the string\'s character.\n            if (!stack.empty() && stack.top() == c) {\n                // Pop a character from the stack.\n                stack.pop();\n            } else {\n                // Otherwise, push that character onto the stack.\n                stack.push(c);\n            }\n        }\n\n        // Initialize a string to construct the result\n        string result;\n        // Pop all characters from the stack and append them to the result\n        while (!stack.empty()) {\n            result.push_back(stack.top());\n            stack.pop();\n        }\n\n        // Since the characters were added in reverse order, \n        // reverse the result to get the final result\n        reverse(result.begin(), result.end());\n        return result;\n    }\n};\n\n```
['Python', 'C++', 'Java', 'Python3', 'JavaScript']