Sunday , 23 April 2017
Home » Class 10 » Program to Remove Duplicate Characters from a Word

Program to Remove Duplicate Characters from a Word

Question:

Write a program to input a word from the user and remove the duplicate characters present in it.

Example:

INPUT – abcabcabc
OUTPUT – abc

INPUT – javaforschool
OUTPUT – javforschl

INPUT – Mississippi
OUTPUT – Misp

Programming Code:

/**
 * The class RemoveDupChar inputs a word and removes duplicate characters
 * @author : www.javaforschool.com
 * @Program Type : BlueJ Program - Java
 */

import java.io.*;
class RemoveDupChar
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter any word : ");
        String s = br.readLine();
        int l = s.length();
        char ch;
        String ans="";
        
        for(int i=0; i<l; i++)
        {
            ch = s.charAt(i);
            if(ch!=' ')
                ans = ans + ch;
            s = s.replace(ch,' '); //Replacing all occurrence of the current character by a space
        }

       System.out.println("Word after removing duplicate characters : " + ans);
    }
}

Output:

Example 1:
Enter any word : Mississippi
Word after removing duplicate characters : Misp

Example 2:
Enter any word : Attitude
Word after removing duplicate characters : Atiude

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

2 comments

  1. Its just too good. Thanks for providing such a good explanation. Just loving this site.☺😊😆

  2. Thannxx.. A greatt site.. Jusst loviingg iiiitt..

Leave a Reply

Your email address will not be published. Required fields are marked *