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 Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e 7 min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read TCP/IP Model The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie 7 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br 14 min read Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam 3 min read Like