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 Python | Count the Number of matching characters in a pair of string The problem is about finding how many characters are the same in two strings. We compare the strings and count the common characters between them. In this article, we'll look at different ways to solve this problem.Using Set Sets are collections of unique items, so by converting both strings into se 2 min read Python | Check if frequencies of all characters of a string are different Given a string S consisting only of lowercase letters, the task is to check if the frequency of all characters of the string is unique. Examples: Input : abaccc Output : Yes âaâ occurs two times, âbâ occurs once and âcâ occurs three times. Input : aabbc Output : No Frequency of both 'a' and 'b' are 3 min read Find repeated character present first in a string Given a string, find the repeated character present first in the string.(Not the first repeated character, found here.) Examples: Input : geeksforgeeks Output : g (mind that it will be g, not e.) Asked in: Goldman Sachs internship Simple Solution using O(N^2) complexity: The solution is to loop thro 15 min read Python - Check if String Contain Only Defined Characters using Regex In this article, we are going to see how to check whether the given string contains only a certain set of characters in Python. These defined characters will be represented using sets. Examples: Input: â657â let us say regular expression contains the following characters- (â78653â) Output: Valid Exp 2 min read Python code to print common characters of two Strings in alphabetical order Given two strings, print all the common characters in lexicographical order. If there are no common letters, print -1. All letters are lower case. Examples: Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e(2 times), g(1 2 min read Like