Wednesday, 1 July 2026

๐Ÿš€ Day 77/150 – Find Duplicate Characters in a String in Python

 



๐Ÿš€ Day 77/150 – Find Duplicate Characters in a String in Python

Strings are one of the most commonly used data types in Python, and a frequent interview or practice question is finding duplicate characters in a string. Duplicate characters are those that appear more than once in the given text.

In this blog, we'll explore multiple ways to identify duplicate characters in a string using Python.

Method 1 – Using a Dictionary

A dictionary can store the frequency of each character. Once the count is calculated, we can print characters whose frequency is greater than 1.

text = "programming" freq = {} for ch in text: freq[ch] = freq.get(ch, 0) + 1 for ch, count in freq.items(): if count > 1: print(ch)





Output

r
g
m

Why Use This Method?

  • Efficient and easy to understand
  • Works well for large strings
  • Time Complexity: O(n)

Method 2 – Taking User Input

This method allows users to enter their own string and find duplicate characters dynamically.

text = input("Enter a string: ") freq = {} for ch in text: freq[ch] = freq.get(ch, 0) + 1 for ch, count in freq.items(): if count > 1: print(ch)










Example Input

hello world

Output

l
o


Method 3 – Using Nested Loops

This approach compares each character with the remaining characters in the string.

text = "programming" duplicates = [] for i in range(len(text)): for j in range(i + 1, len(text)): if text[i] == text[j] and text[i] not in duplicates: duplicates.append(text[i]) print(duplicates)





Output

['r', 'g', 'm']

Pros
  • No dictionary required
  • Useful for understanding string comparisons

Cons

  • Less efficient for larger strings
  • Time Complexity: O(n²)

Method 4 – Using Set and count()

A concise approach is to use a set to get unique characters and count their occurrences.

text = "programming" for ch in set(text): if text.count(ch) > 1: print(ch)






Output

r
g
m

Pros
  • Short and readable
  • Easy to implement

Cons

  • count() scans the string repeatedly
  • Not ideal for very large strings

๐ŸŽฏ Real-World Applications

Finding duplicate characters is useful in:

  • Data validation
  • Text processing
  • Password analysis
  • Frequency analysis
  • Coding interviews and programming challenges

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (299) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (12) BI (10) Books (263) Bootcamp (12) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (32) data (7) Data Analysis (38) Data Analytics (26) data management (16) Data Science (379) Data Strucures (22) Deep Learning (186) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (21) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (74) Git (12) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (331) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (14) PHP (20) Projects (34) Python (1395) Python Coding Challenge (1176) Python Mathematics (1) Python Mistakes (51) Python Quiz (556) Python Tips (19) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (20) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)