Home / Blue J / Java program to remove duplicate characters

Java program to remove duplicate characters

Java program to remove duplicate characters or repeated character from a given string.

Example: Thiiis is aa tessstt ssstring
OUTPUT:  This is a test string

Thus our motive is to remove repeated characters or duplicate characters from a given string. This is done by following concept.

Step #1: select the starting character:   ch=str.charAt(0);    (hhhello)
Step #2: Add the character to the resulting/output String: ans = ans + ch;
Step #3: Now until the same character appears skip to add the character in the output string:
while( s.charAt(++i)==ch)
Step #4: Do this uptil i < l ; means for whole length of string.

Program #1:

// Java program to remove duplicate/repeated characters from a string  www.getprogramcode.com
import java.util.*;
class RemoveDuplicate
{
    public static void main(String args[])
    {
	String s,ans="";
	char ch	;
	int l,i=0;
	Scanner sc=new Scanner(System.in);
        System.out.print("Enter any string: "); // Inputting the word
        s = sc.nextLine();
	l=s.length();
	s+=" ";			// *Adding extra space at the end because last character is compared with something else index out of bound error.
	ch=s.charAt(0);	// taking ch as first character
	while(i<l)
	{			 
		ans= ans+ ch;  // adding each individual character to the answer string or output string without repeated characters

		while(s.charAt(++i)==ch && i<l)
		{}
	ch=s.charAt(i);	// **to store the previously last character in ch
	}

	System.out.println("String after removing repeated characters : \n"+ans); // Printing the string without duplicate characters
	}
    }

 OUTPUT:

Program to remove duplicate characters
Java program to remove duplicate/repeated characters from a string

Program #2:  Small change in inner while loop;

import java.util.*;
class RemoveDuplicate
{
    public static void main(String args[])
    {
	String s,ans="";
	char ch1,ch2;
	int l,i=0;
	Scanner sc=new Scanner(System.in);
        System.out.print("Enter any string: "); // Inputting the word
        s = sc.nextLine();
	l=s.length();
	s+=" ";			// *Adding extra space at the end because last character is compared with something else index out of bound error.
	ch1=s.charAt(0);	// taking ch1 as first character
	while(i<l)
	{			 
		ans= ans+ ch1;  // adding each individual character to the answer string or output string without repeated characters

		while(ch1==(ch2=s.charAt(++i)) && i<l)
		{}
	ch1=ch2;	// **to store the previously last character in ch1
	}

	System.out.println("String after removing repeated characters : \n"+ans); // Printing the string without duplicate characters
	}
    }

 OUTPUT:

Program to remove duplicate characters
Java program to remove duplicate/repeated characters from a string

Incoming terms: Program to remove duplicate or repeated characters from a string.

Check Also

Program to check Upper Triangular matrix

What is Upper Triangular Matrix?  An upper triangular matrix is a square matrix in which ...

Leave a Reply

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

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.