Data structures | SoloLearn: Learn to code for FREE!

+2

Data structures

Write a function called remove_duplicates which will take one argument called string. This string input will only have characters between a-z. The function should remove all repeated characters in the string and return a tuple with two values: A new string with only unique, sorted characters. The total number of duplicates dropped. For example: remove_duplicates('aaabbbac') => ('abc', 5) remove_duplicates('a') => ('a', 0) remove_duplicates('thelexash') =>

10/11/2017 9:06:25 PM

Franklin Yaw Frimpong

1 Answer

New Answer

+1

def remove_duplicates(string): list = {} duplicates = 0 for char in string: if char in list: list[char] = list.get(char) + 1 duplicates = duplicates + 1 else: list[char] = 0 print('(\'' + ''.join(list.keys()) + '\', ' + str(duplicates) + ')')