Signup/Sign In
PUBLISHED ON: JULY 5, 2021

Python Progam to Replace duplicate Occurrence in String

In this tutorial, you will learn to replace duplicate occurrences from a string. Occurrence is the total number of times a word or a character has occurred in the string. Strings in Python are a sequence of characters wrapped in single, double, or triple quotes. For a given string, we have to check if any word occurs more than once in the string and replace the duplicated word with a suitable word.

Let us look at the sample input and output of the program.

Input: "Python is good Python is easy"

Output: Python is good it is easy

To solve this problem in Python we can follow these approaches:

  1. By using enumerate(), split() and loop
  2. By using keys(), index() and list comprehension

Approach 1: Using enumerate(), split() and loop

In this approach, we will separate the words and then will memorize the first occurrence in the set and check if the value has already occurred before or not. Words that are repeated will be replaced.

The split () method is used to split a string into an array of substrings, and the enumerate() method adds a counter to an iterable and returns it in a form of an enumerate object.

To iterate through the string, we will be using a loop.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise test string

Step 2- Declare a dictionary that will store the words and their replacement

Step 3- Get words of the string using split()

Step 4- Check if any word which has already occurred is found

Step 5- Replace such words using the dictionary mapping

Step 6- Add the replaced word in the list

Step 7- Get the resultant string by joining words in the list

Step 8- Print the string as the result

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. We will use a dictionary to store the replacement mapping of the words. To split the string and get words we have used space (' ') as the separator.

To add elements in a set we have used add() method. To get the resultant string, we have joined the words after replacement in the list.

test_string = 'Betty is a girl. Betty is good in Math , Math is her favourite subject.'
  
print("The original string is : " + str(test_string))
replace_dict = {'Betty' :  'She', 'Math' : 'that' }

test_list = test_string.split(' ')
r = set()
for i, el in enumerate(test_list):
    if el in replace_dict:
        if el in r:
            test_list[i] = replace_dict[el]
        else:
            r.add(el)
r = ' '.join(test_list)
  
# printing result 
print("The string after replacing : " + str(r)) 


The original string is : Betty is a girl. Betty is good in Math , Math is her favourite subject.
The string after replacing : Betty is a girl. She is good in Math , that is her favourite subject.

Approach 2: keys(), index() and list comprehension

In this approach, we will follow a linear approach of searching and replacing duplicate words. We will use a dictionary to store the replacement mapping of the words.

The keys() method returns an object which stores keys of the dictionary, as a list, and the index() method returns the position at the first occurrence of the specified value.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise test string

Step 2- Declare a dictionary that will store the words and their replacement

Step 3- Get words of the string using split() and store in a list

Step 4- Declare another result list to store the words after replacement

Step 5- Use list comprehension for a shorter syntax for writing replacement code

Step 6- Convert List to string

Step 7- Print the result string

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach. To convert a list to a string, we have used the str() method.

test_string = 'James is good in Science , Science is James favourite subject. '
  
# printing original string
print("The original string is : " + str(test_string))
  
# initializing replace mapping 
repl_dict = {'James' :  'his', 'Science' : 'it' }
  
# Replace duplicate Occurrence in String
# Using keys() + index() + list comprehension
test_list = test_string.split(' ')
res = ' '.join([repl_dict.get(val) if val in repl_dict.keys() and test_list.index(val) != index 
                                   else val for index, val in enumerate(test_list)])
print("The string after replacing : " + str(res))


The original string is : James is good in Science , Science is James favourite subject.
The string after replacing : James is good in Science , it is his favourite subject.

Conclusion

In this tutorial, we have learned two different approaches for replacing duplicate words from a string. We have used a combination of built-in functions and different traversing approaches to get the final string.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.