Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Remove Consecutive Duplicates

Easy Acceptance 60% Points 20

You are given a string s. Your task is to remove all consecutive duplicate characters. If the same characters appear next to each other, keep only one occurrence. Return the final string after removing duplicates.

Examples
Example 1

image Input: s = "aabbccdaa"

Output: "abcda"

Explanation: Consecutive duplicate characters are removed.

Example 2

image

Input: s = "aaaa"

Output: "a"

Explanation: All duplicates are consecutive, so only one character remains.

Example 3

image

Input: s = "abca"

Output: "abca"

Explanation: No consecutive duplicates are present, so the string remains unchanged.

Constraints
  • 1 <= length of s <= 10^5
  • The string contains lowercase or uppercase letters only
Topics
Recursion
Companies
Apple Cognizant
Added 09 Aug 2026 60% acceptance
Ln 1, Col 1Sign in to save your code
aaabbcdd
abcd