generate functionMon, 10 Apr 2023

contar la cantidad de caracteres diferentes que aparecen más de una vez en una cadena

def char_count(text): # create a hashtable that counts the character frequency char_frequency = {} # iterate through the characters in the text for char in list(text): # if the character is in the hashtable, increment the frequency if char in char_frequency: char_frequency[char] += 1 # if not in the hashtable, insert the character and set the frequency to 1 else: char_frequency[char] = 1 # create a list that holds the characters that appear more than once and their counts duplicate_chars = [] for key, value in char_frequency.items(): # if the frequency is greater than 1, append the character and its frequency to the list if value > 1: duplicate_chars.append((key, value)) # return the list return duplicate_chars

Questions about programming?Chat with your personal AI assistant