Program to print duplicate characters in the string using Python

Introduction

The task is to find and print duplicate characters in the given string.

Program to print duplicate characters in the string

Program

from collections import Counter


def duplicate_char(ip_str):
    ip_str = ip_str.lower()
    word_counter = Counter(ip_str)
    for key,value in word_counter.items():
        if value > 1:
            print(key)
           
ip_str =input("Enter the string: ")
print("The duplicate characters in the given string are:")
duplicate_char(ip_str)

 

Output

Program to print duplicate characters in the string Output

Explanation

In the above python code, we have used Counter() to get the key, value of characters present in the input string. We checked key and values for items and printed the key whose value is greater than 1. The key contains the unique characters and value contains the occurrence of characters.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.