Python Practice › String Programs › Program 76

Characters that occur more than once

EasyString Programs · 76 of 106

Write a Python program to build a string of the characters that occur more than once.

Advertisement
Input
"aabbcceffgh"
Output
abcf

Approach

  1. Count, then keep characters with a count above 1, in first-seen order.

Solution

Python
from collections import Countertext = "aabbcceffgh"counts = Counter(text)print("".join(ch for ch in counts if counts[ch] > 1))
Output
abcf

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Leave a Comment