Remove Duplicate Character from String in Python

Remove Duplicate Character from String in Python


In this Blog we are going to learn how to remove Duplicate Character from string in python




def remove_duplicates(s):

  # Create a set of the characters in the string

  chars = set(s)

  # Join the set elements into a new string

  no_duplicates = ''.join(chars)

  return no_duplicates

# Test the function

print(remove_duplicates('hello'))  # Output: "helo"

print(remove_duplicates('abcdefg'))  # Output: "abcdefg"

print(remove_duplicates('aaabbbccc'))  # Output: "abc" 


Output 

ehlo

ebfadgc

cab

Post a Comment

0 Comments