Remove Duplicate Characters from a Word Program in Java and Python
Remove duplicate characters from a word program with algorithm, explanation, Java solution and simple Python solution.
Question:
Write a program to input a word from the user and remove the duplicate characters present in it.
Algorithm:
Step 1: Start.
Step 2: Accept a word from the user.
Step 3: Store the length of the word in len.
Step 4: Initialize an empty string answer.
Step 5: Run a loop from index 0 to len - 1.
Step 6: Extract the character at the current index.
Step 7: If the character is not a blank space, append it to answer.
Step 8: Replace all occurrences of that character in the word by blank spaces.
Step 9: Continue the loop. Later duplicate positions will contain blank spaces and will not be added again.
Step 10: Display answer as the word after removing duplicate characters.
Step 11: Stop.
Explanation:
This program removes duplicate characters by preserving only the first occurrence of every character. The important idea is that the string is not checked by comparing each character with every previous character. Instead, whenever a character is accepted into the answer, all occurrences of that same character in the working string are replaced by blank spaces. This makes the later duplicate occurrences harmless because they will be skipped when the loop reaches them.
The variable answer begins as an empty string. The loop reads the word from left to right. If the current character is not a blank space, it means this character has not already been removed due to an earlier occurrence. Therefore it is the first occurrence that should be kept, and the program appends it to answer. Immediately after that, replace() changes all occurrences of that character in the word to blank spaces.
For example, in Mississippi, the first character M is added to the answer and all M characters are replaced. The next new character is i, so it is added and all i characters are replaced. The same process happens for s and p. When the loop reaches later positions where duplicate i, s or p occurred earlier, those positions now contain blank spaces, so nothing is added again. The final result becomes Misp.
This method is case-sensitive because Java treats uppercase and lowercase letters as different characters. So M and m would not be considered the same unless the word is converted to one case before processing. The program keeps the original case because the question asks only to remove duplicate characters, not to change the word. The logic is simple and suitable for ICSE string programs because it uses only a loop, charAt(), string concatenation and replace().
Java Program:
/**
* The class RemoveDupChar inputs a word and removes duplicate characters.
* Only the first occurrence of each character is kept.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class RemoveDupChar
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter any word: ");
String word = sc.nextLine();
int len = word.length();
String answer = "";
for(int i = 0; i < len; i++)
{
char ch = word.charAt(i);
if(ch != ' ')
{
// The first occurrence of the character is added to the answer.
answer = answer + ch;
}
/*
* All occurrences of the current character are replaced by spaces.
* Later duplicates will therefore be skipped by the if condition.
*/
word = word.replace(ch, ' ');
}
System.out.println("Word after removing duplicate characters: " + answer);
}
}Equivalent Python Program:
word = input("Enter any word: ")
length = len(word)
answer = ""
for i in range(0, length):
ch = word[i]
if ch != " ":
# The first occurrence of the character is stored.
answer = answer + ch
# Replace all occurrences of the current character by spaces.
# Later duplicates will be skipped when the loop reaches them.
word = word.replace(ch, " ")
print("Word after removing duplicate characters:", answer)Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.