Java Program to Remove Duplicate Characters from a Word


Introduction


This program helps to remove duplicate character from the input string.

Like we passed String as “Dharmendra” and a is duplicate in the string so result will be “Dharmend“.


Flowchart


Please find sample flowchart diagram for this program as given below –


Program(Code) Using Java Language


package com.kw.sample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * This program helps to remove duplicate character from the input string.
 *
 * @author dsahu1
 *
 */
public class KWRemoveDuplicateCharExample {

    public static void main(String[] args) throws IOException {
        // Instantiate BufferedReader object to take input string
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print(“Enter any word : “);
        String str = br.readLine();
        int len = str.length();
        char ch;
        String result = “”;

        // Iterate String
        for (int i = 0; i < len; i++) {
            // Pick character
            ch = str.charAt(i);
            if (ch != ‘ ‘) {
                result = result + ch;
            }
            str = str.replace(ch, ‘ ‘);
        }
        System.out.println(” String after Duplicate :” + result);
    }

}

 


Output


Scenario 1:

Enter any word : Dharmendra
 String after Duplicate :Dharmend

Scenario 2:

Enter any word : My Name is Dharmendra!
 String after Duplicate :MyNameisDhrnd!


Video Explanation


This video helps to understand flowchart and program which written in the above steps. There are various ways to write a program and we are not covering all of them as this is just for practice. Please post your program in the comment box so other can utilize it.

Leave a Reply

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