Find all duplicate characters in string in Python Last Updated : 20 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will explore various methods to find all duplicate characters in string. The simplest approach is by using a loop with dictionary.Using Loop with DictionaryWe can use a for loop to find duplicate characters efficiently. First we count the occurrences of each character by iterating through the string and updating a dictionary. Then we loop through the dictionary to identify characters with a frequency greater than 1 and append them to the result list. Python s = "GeeksforGeeks" d = {} res = [] # Count characters for c in s: d[c] = d.get(c, 0) + 1 # Find duplicate for c, cnt in d.items(): if cnt > 1: res.append(c) print(res) Output['G', 'e', 'k', 's'] Explanation:Use a dictionary (d) to store the frequency of each character.Check if the count of any character is greater than 1 (duplicates) then add into res listNote: This method is better for most cases due to its efficiency (O(n)) and simplicity. Let's explore other different methods to find all duplicate characters in string:Table of ContentUsing count()Using collections.CounterUsing count()The count() method can be used to determine the frequency of each character in the string directly. While this approach is simple but it is less efficient for larger strings due to repeated traversals. Python s = "GeeksforGeeks" res = [] # Iterate over the unique elements in 's' for c in set(s): # Use set to avoid repeated checks if s.count(c) > 1: res.append(c) print(res) Output['G', 'k', 'e', 's'] Explanation:We use a set() to loop through unique characters only. This will avoiding redundant checks.For each unique character s.count(c) counts how many times it appears in the string.If count is greater than 1 then character is added to the res list.Note: This method is easy to use but inefficient for large strings (O(n2)). Use only for small inputs.Using collections.CounterThe collections.Counter module provides a simple way to count occurrences of elements in a string. Python from collections import Counter s = "GeeksforGeeks" # Create a Counter object to count occurrences # of each character in string d = Counter(s) # Create a list of characters that occur more than once res = [c for c, cnt in d.items() if cnt > 1] print(res) Output['G', 'e', 'k', 's'] Explanation:The Counter() function counts each character in the string.Use a list comprehension to extract characters with a count greater than 1.Note: This method is more concise and efficient (O(n)). Comment More infoAdvertise with us Next Article Find all duplicate characters in string in Python A AFZAL ANSARI Follow Improve Article Tags : Misc Python python-string Python string-programs Practice Tags : Miscpython Similar Reads Find one extra character in a string Given two strings which are of lengths n and n+1. The second string contains all the characters of the first string, but there is one extra character. Your task is to find the extra character in the second string. Examples: Input : string strA = "abcd"; string strB = "cbdae"; Output : e string B con 15+ min read Count occurrences of a character in string in Python We are given a string, and our task is to count how many times a specific character appears in it using Python. This can be done using methods like .count(), loops, or collections.Counter. For example, in the string "banana", using "banana".count('a') will return 3 since the letter 'a' appears three 2 min read Remove All Duplicates from a Given String in Python The task of removing all duplicates from a given string in Python involves retaining only the first occurrence of each character while preserving the original order. Given an input string, the goal is to eliminate repeated characters and return a new string with unique characters. For example, with 2 min read Count the number of Unique Characters in a String in Python We are given a string, and our task is to find the number of unique characters in it. For example, if the string is "hello world", the unique characters are {h, e, l, o, w, r, d}, so the output should be 8.Using setSet in Python is an unordered collection of unique elements automatically removing du 2 min read Concatenated string with uncommon characters in Python The goal is to combine two strings and identify the characters that appear in one string but not the other. These uncommon characters are then joined together in a specific order. In this article, we'll explore various methods to solve this problem using Python.Using set symmetric difference We can 3 min read Like