Python Program to Remove Characters that Appear More than k Times

In the previous article, we have discussed Python Program to Remove Odd Occurring Characters from the String

Given a string, K value and the task is to remove all the characters from the given string that appears more than k times.

Examples:

Example1:

Input:

Given String = "goodmorningall"
Given k value = 2

Output:

The given string { goodmorningall } after removal of all characters that appears more than k{ 2 } times : dmria

Example2:

Input:

Given String = "hellobtechgeeks"
Given k value= 3

Output:

The given string { hellobtechgeeks } after removal of all characters that appears more than k{ 3 } times : hllobtchgks

Program to Remove Characters that Appear More than k Times in Python

Below are the ways to remove all the characters from the given string that appears more than k times in python:

Method #1: Using Dictionary (Hashing, Static Input)

Approach:

  • Take a dictionary and initialize it to empty using the {} or dict() say freqncyDictionary.
  • Give the string as static input and store it in a variable.
  • Loop in the given string using the For loop.
  • Inside the For loop, Check if the string character is present in the dictionary or not using the if conditional statement and ‘in’ keyword.
  • If it is true then increment the count of the string character in the dictionary by 1.
  • Else initialize the dictionary with the string character as key and value as 1.
  • Give the k value as static input and store it in a variable.
  • Take a string that stores all the characters which are not occurring even a number of times and initialize it to null string using “or str()
  • loop in the given string using the for loop.
  • Check if the character has a frequency less than k by checking the value of that character in the frequency dictionary.
  • we check using the if conditional statement
  • If it is true then concatenate this character to modifd_string using string concatenation.
  • Print the modifd_string string.
  • The Exit of the Program.

Below is the implementation:

# Take a dictionary and initialize it to empty
# using the {} or dict() say freqncyDictionary.
freqncyDictionary = {}
# Give the string as static input and store it in a variable
gvnstrng = "hellobtechgeeks"
# Loop in the given string using the For loop.
for i in gvnstrng:
        # Inside the For loop,
    # Check if the string character is present in the dictionary
    # or not using the if conditional statement and 'in' keyword.
    if i in freqncyDictionary.keys():
                # If it is true then increment the count of the string character
        # in the dictionary by 1.
        freqncyDictionary[i] = freqncyDictionary[i]+1
    # Else initialize the dictionary with the string character as key and value as 1.
    else:
        freqncyDictionary[i] = 1
# Give the k value as static input and store it in a variable.
k = 3
# Take a string which stores all the characters which are not occuring even number
# of times and initialize it to null string using "" or str()
modifd_string = ""
# loop in the given string using the for loop
for charac in gvnstrng:

        # check if the character has frequency less than k by checking value of that character in frequency dictionary
        # we check using the if conditional statement
    if(freqncyDictionary[charac] < k):
        # if it is true then concatenate this character to modifd_string using string concatenation
        modifd_string = modifd_string+charac

# print the modifd_string string
print('The given string {', gvnstrng,
      '} after removal of all characters that appears more than k{', k, '} times :', modifd_string)

Output:

The given string { hellobtechgeeks } after removal of all characters that appears more than k{ 3 } times : hllobtchgks

Method #2: Using Dictionary (Hashing, User Input)

Approach:

  • Take a dictionary and initialize it to empty using the {} or dict() say freqncyDictionary.
  • Give the string as the user input using the input() function and store it in a variable.
  • Loop in the given string using the For loop.
  • Inside the For loop, Check if the string character is present in the dictionary or not using the if conditional statement and ‘in’ keyword.
  • If it is true then increment the count of the string character in the dictionary by 1.
  • Else initialize the dictionary with the string character as key and value as 1.
  • Give the k value as user input using the int(input()) function and store it in a variable.
  • Take a string that stores all the characters which are not occurring even a number of times and initialize it to null string using “or str()
  • loop in the given string using the for loop.
  • Check if the character has a frequency less than k by checking the value of that character in the frequency dictionary.
  • we check using the if conditional statement
  • If it is true then concatenate this character to modifd_string using string concatenation.
  • Print the modifd_string string.
  • The Exit of the Program.

Below is the implementation:

# Take a dictionary and initialize it to empty
# using the {} or dict() say freqncyDictionary.
freqncyDictionary = {}
# Give the string as the user input using the input() function and store it in a variable.
gvnstrng = input("Enter some random string = ")
# Loop in the given string using the For loop.
for i in gvnstrng:
        # Inside the For loop,
    # Check if the string character is present in the dictionary
    # or not using the if conditional statement and 'in' keyword.
    if i in freqncyDictionary.keys():
                # If it is true then increment the count of the string character
        # in the dictionary by 1.
        freqncyDictionary[i] = freqncyDictionary[i]+1
    # Else initialize the dictionary with the string character as key and value as 1.
    else:
        freqncyDictionary[i] = 1
# Give the k value as user input using the int(input()) function and store it in a variable.
k = int(input("Enter some random number ="))
# Take a string which stores all the characters which are not occuring even number
# of times and initialize it to null string using "" or str()
modifd_string = ""
# loop in the given string using the for loop
for charac in gvnstrng:

        # check if the character has frequency less than k by checking value of that character in frequency dictionary
        # we check using the if conditional statement
    if(freqncyDictionary[charac] < k):
        # if it is true then concatenate this character to modifd_string using string concatenation
        modifd_string = modifd_string+charac

# print the modifd_string string
print('The given string {', gvnstrng,
      '} after removal of all characters that appears more than k{', k, '} times :', modifd_string)

Output:

Enter some random string = goodmorningall
Enter some random number =2
The given string { goodmorningall } after removal of all characters that appears more than k{ 2 } times : dmria

Method #3: Using Counter() function (Hashing, Static Input)

Approach:

  • Import the Counter() function from collections using the import keyword.
  • Give the string as static input and store it in a variable
  • Calculate the frequency of all the given string characters using the Counter() function which returns the element and its frequency as key-value pair  and store this dictionary in a variable(say freqncyDictionary)
  • Give the k value as static input and store it in a variable.
  • Take a string that stores all the characters which are not occurring even a number of times and initialize it to null string using “or str()
  • loop in the given string using the for loop.
  • Check if the character has a frequency less than k by checking the value of that character in the frequency dictionary.
  • we check using the if conditional statement
  • If it is true then concatenate this character to modifd_string using string concatenation.
  • Print the modifd_string string.
  • The Exit of the Program.

Below is the implementation:

# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the string as static input and store it in a variable
gvnstrng = "hellobtechgeeks"
# Calculate the frequency of all the given string characters using the Counter()
# function which returns the element and its frequency as key-value pair
# and store this dictionary in a variable(say freqncyDictionary)
freqncyDictionary = Counter(gvnstrng)
# Give the k value as static input and store it in a variable.
k = 3
# Take a string which stores all the characters which are not occuring even number
# of times and initialize it to null string using "" or str()
modifd_string = ""
# loop in the given string using the for loop
for charac in gvnstrng:

        # check if the character has frequency less than k by checking value of that character in frequency dictionary
        # we check using the if conditional statement
    if(freqncyDictionary[charac] < k):
        # if it is true then concatenate this character to modifd_string using string concatenation
        modifd_string = modifd_string+charac

# print the modifd_string string
print('The given string {', gvnstrng,
      '} after removal of all characters that appears more than k{', k, '} times :', modifd_string)

Output:

The given string { hellobtechgeeks } after removal of all characters that appears more than k{ 3 } times : hllobtchgks

Method #4: Using Counter() function (Hashing, User Input)

Approach:

  • Import the Counter() function from collections using the import keyword.
  • Give the string as the user input using the input() function and store it in a variable.
  • Calculate the frequency of all the given string characters using the Counter() function which returns the element and its frequency as key-value pair  and store this dictionary in a variable(say freqncyDictionary)
  • Give the k value as user input using the int(input()) function and store it in a variable.
  • Take a string that stores all the characters which are not occurring even a number of times and initialize it to null string using “or str()
  • loop in the given string using the for loop.
  • Check if the character has a frequency less than k by checking the value of that character in the frequency dictionary.
  • we check using the if conditional statement
  • If it is true then concatenate this character to modifd_string using string concatenation.
  • Print the modifd_string string.
  • The Exit of the Program.

Below is the implementation:

# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the string as the user input using the input() function and store it in a variable.
gvnstrng = input("Enter some random string = ")
# Calculate the frequency of all the given string characters using the Counter()
# function which returns the element and its frequency as key-value pair
# and store this dictionary in a variable(say freqncyDictionary)
freqncyDictionary = Counter(gvnstrng)
# Give the k value as user input using the int(input()) function and store it in a variable.
k = int(input("Enter some random number ="))
# Take a string which stores all the characters which are not occuring even number
# of times and initialize it to null string using "" or str()
modifd_string = ""
# loop in the given string using the for loop
for charac in gvnstrng:

        # check if the character has frequency less than k by checking value of that character in frequency dictionary
        # we check using the if conditional statement
    if(freqncyDictionary[charac] < k):
        # if it is true then concatenate this character to modifd_string using string concatenation
        modifd_string = modifd_string+charac

# print the modifd_string string
print('The given string {', gvnstrng,
      '} after removal of all characters that appears more than k{', k, '} times :', modifd_string)

Output:

Enter some random string = goodmorningall
Enter some random number =2
The given string { goodmorningall } after removal of all characters that appears more than k{ 2 } times : dmria

Dive into numerous Python Programming Language Examples for practice and get the best out of the tutorial and learn python one step at a time.