Find Duplicate characters in a string
Pattern: Strings basics and frequency
Count every character in one pass. In a second pass, report each character whose count is more than 1, once, at its first appearance.
The problem
Given a string, return the characters that appear more than once, each listed once, in the order they first appear.
For example, "geeksforgeeks" gives "geks", and "test string" gives "ts", because t appears three times and s
twice. Spaces count as characters too.
The link above is a GeeksforGeeks article, which also prints how many times each one appears. The function name
duplicates and this exact output (characters only, in first-appearance order) are this guide's own.
Think first
- Can you know a character is a duplicate the first time you see it?
- After counting, how do you report each duplicate only once?
- Which order should the second pass follow?
Hints
Brute force, and why it fails
For each character, scan the rest of the string for another copy, and check that it was not reported already: O(n²). A count array makes it O(n).
The key insight
"Which characters repeat" is a counting question, and "in which order" is a second pass over the string. Clearing a count after use is a cheap way to say "already reported".
Dry run
| counts | g 2, e 4, k 2, s 2, f 1, o 1, r 1 |
|---|
s = geeksforgeeks. First pass, count every character. g 2, e 4, k 2, s 2, f 1, o 1, r 1.
The plan
count = array of 256 zeros
for each c in s: count[c] = count[c] + 1
out = empty builder
for each c in s:
if count[c] > 1: append c to out; count[c] = 0
return out
Complexity
- Time
O(n)- Space
O(1)
Two passes. The count array has a fixed size of 256.
Edge cases
- No repeats, as in
"abc":"". - One character repeated, as in
"aaaa":"a". - Spaces and punctuation are counted like letters.
I've built my logic, show the code
Java
class Solution {
public String duplicates(String s) {
int[] count = new int[256];
for (int i = 0; i < s.length(); i++) count[s.charAt(i)]++; // first pass: count every character
StringBuilder out = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (count[c] > 1) { out.append(c); count[c] = 0; } // report once, at its first place
}
return out.toString();
}
}C++
class Solution {
public:
string duplicates(string s) {
int count[256] = {0};
for (unsigned char c : s) count[c]++; // first pass: count every character
string out;
for (unsigned char c : s) {
if (count[c] > 1) { out += static_cast<char>(c); count[c] = 0; } // report once, at its first place
}
return out;
}
};Python
class Solution:
def duplicates(self, s: str) -> str:
count = {}
for c in s: # first pass: count every character
count[c] = count.get(c, 0) + 1
out = []
for c in s:
if count[c] > 1:
out.append(c)
count[c] = 0 # report once, at its first place
return "".join(out)Common mistakes
- Reporting a character every time it appears after the first.
- Using a 26-slot array when the string can contain spaces or capitals.
- Printing in alphabetical order when the question asks for first-appearance order (or the other way round).