# How would you remove duplicates in a string?

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/3814/remove-duplicates-in-string
Source observed: 2026-11-04T22:21:39.960Z
Last checked: 2026-11-04T22:21:39.96+00:00
Indexed: 2026-11-05T20:22:13.795+00:00

---

[All Questions](https://www.aced.io/questions)

# How would you remove duplicates in a string?

**Difficulty:** Medium  
**Category:** [Data Structures & Algorithms](https://www.aced.io/questions?type=algorithms) · [Coding](https://www.aced.io/questions?type=coding)  
**Reported:** 3 interviews; last asked 2 months ago  
**Roles:** [Software Engineer](https://www.aced.io/questions?role=swe)  
**Companies:** [Amazon](https://www.aced.io/questions?company=amazon), [Cognizant](https://www.aced.io/questions?company=cognizant)

## Question

**Remove Adjacent Duplicates in String.** You are given a string `s` and an integer `k`. Write a function to remove `k` adjacent duplicates from `s`, where the adjacent characters are equal.

For example, if `k = 3` and `s = "daaabbbaa"`, the string contains the adjacent triples `"aaa"` and `"bbb"`. The function should transform it to `"daa"`, removing `"bbb"` first and then the remaining `"aaa"`.

## Examples

```text
s = 'abcd'
k = 2
output: 'abcd'

s = 'deeedbbcccbdaa'
k = 3
output: 'aa'

s = 'pbbcggttciiippooaais'
k = 2
output: 'ps'

s = 'aaabbbacd'
k = 3
output: 'acd'
```

In every example, `output` is the resulting string after all possible groups of `k` adjacent duplicates have been removed.

## Page actions and available views

The page includes **I was asked this**, **Save 107**, and **More actions** controls. The question view is available alongside **Solution**, **Mock Interview**, and **Community 58** views. The solution and mock-interview tabs are locked in the captured interface. The coding workspace offers Python selection, **Editor** and **Share** tabs, **Output**, **Test Results**, and **Console** panels. The editor was still loading in the captured interface. [Sign up to run code](https://www.aced.io/signup?src=lesson)

Detailed company statistics are available through [Upgrade](https://www.aced.io/upgrade?src=questions).

## Interview experiences

Two experiences are shared:

- [Amazon · 2 months ago](https://www.aced.io/experiences/amazon-software-engineer-intern-interview-d74e7f) — “It was well, interviewer was helpful and was asking mostly standard questions from leetcode. I prepared from Strivers A2Z sheet so it was enough. I could have explained my thinking better.” **Software Engineer · Intern**
- [Cognizant · a year ago](https://www.aced.io/experiences/cognizant-software-engineer-interview-fb7044) — “The Process was easy, even though I am in my final year of B.Tech. I would say basics from multiple domains or tech stacks are enough to get into Cognizant for freshers.” **Software Engineer · Entry Level / L3**

## Related questions

- [How would you implement retries without causing duplicate operations?](https://www.aced.io/questions/6644/implement-retries-prevent-duplicate-operations)
- [Remove duplicates in an array of objects.](https://www.aced.io/questions/5441/remove-duplicates-array-objects)
- [How would you improve Rover?](https://www.aced.io/questions/2607/improve-rover-strategies)

## Related courses

### [Software Engineering Interview Prep](https://www.aced.io/courses/software-engineering)

![Software Engineering Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/4VkuYewBHUFIVUS5oJDW5h/02e833439b1289f25f38f13f81cbdca3/adobe_stock_optimized.jpeg?w=800&h=800&fm=webp&q=75)

We help candidates prepare for software engineering roles at Google, Amazon, Microsoft, Meta, Apple, and other top companies through mock interviews, frameworks, senior-candidate advice, data structures, algorithms, system design, people management, and behavioral-interview practice.

### [System Design Interviews](https://www.aced.io/courses/system-design-interviews)

![System Design Interviews](https://images.ctfassets.net/x78yjrjc11pq/78x7miFZFjBCrM5GqftrMJ/2f6d26e632c4e7265c49f6a180227edb/system_design_track_compressed_26ac98a46e__281_29_5be2280f31.jpeg?w=800&h=800&fm=webp&q=75)

We teach approaches to current system-design questions across product, infrastructure, and AI domains with in-depth video examples, written breakdowns, reference patterns, and mock-interview videos featuring senior engineers and managers.

[Sign up](https://www.aced.io/signup?src=nav) or [log in](https://www.aced.io/login?src=nav) to use Aced’s interview-preparation tools.

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F3814%2Fremove-duplicates-in-string&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F3814%2Fremove-duplicates-in-string). Opening a form does not execute a tool.