- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find All Duplicate Characters from a String using Python
One String is given. Our task is to find those characters whose frequency is more than one in the given string.
As an example, we can see that the string “Hello World. Let’s learn Python” so the algorithm will find those letters which are occurring multiple times. In this case, the output will look like this -
e : 3 l : 4 o , 3) <space> : 4 r : 2 t : 2 n : 2
To implement this problem we are using Python Collections. From the collection, we can get the Counter() method. The Counter() method is used to count the hashtable objects. In this case, it separates the characters from the text and makes each character as a key of the dictionary, and the character count is the value of those keys.
Algorithm
Step 1: Find the key-value pair from the string, where each character is key and character counts are the values. Step 2: For each key, check whether the value is greater than one or not. Step 3: If it is greater than one then, it is duplicate, so mark it. Otherwise, ignore the character
Example Code
from collections import Counter defcalc_char_freq(string): freq_count = Counter(string) # get dictionary with letters as key and frequency as value for key in freq_count.keys(): if freq_count.get(key) > 1: # for all different keys, list the letters and frequencies print("(" + key + ", " + str(freq_count.get(key)) + ")") myStr = 'Hello World. Let’s learn Python' calc_char_freq(myStr)
Output
(e, 3) (l, 4) (o, 3) ( , 4) (r, 2) (t, 2) (n, 2)
- Related Articles
- Python program to find all duplicate characters in a string
- Java program to find all duplicate characters in a string
- Program to remove duplicate characters from a given string in Python
- JavaScript Remove non-duplicate characters from string
- Program to find string after removing consecutive duplicate characters in Python
- Java Program to find duplicate characters in a String?
- C# Program to remove duplicate characters from String
- Java program to delete duplicate characters from a given String
- How to print duplicate characters in a String using C#?
- Program to find string after deleting k consecutive duplicate characters in python
- Java Program to Find the Duplicate Characters in a String
- Find the smallest window in a string containing all characters of another string in Python
- How to remove all special characters, punctuation and spaces from a string in Python?
- Removing all non-alphabetic characters from a string in JavaScript
- Checking if a string contains all unique characters using JavaScript

Advertisements