Remove Duplicate Characters
C++
Hard
4 views
Problem Description
Remove all duplicate characters, keep only first occurrence.
Real Life: Unique character extraction.
Step-by-Step Logic:
1. Track which characters already seen
2. Add character to result only if not seen before
3. Mark character as seen
4. Return unique character string
Official Solution
void string_q12_remove_duplicates() {
string text = "programming";
string result = "";
bool seen[256] = {false};
cout << "Original: " << text << endl;
for(int i = 0; i < text.length(); i++) {
if(!seen[text[i]]) {
result += text[i];
seen[text[i]] = true;
}
}
cout << "After removing duplicates: " << result << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!