Java Program to remove the duplicate characters from the string
import java.util.Scanner;
class RemoveDuplicateCharacter
{
public static void main(String[] arg)
{
//Ask user to inter a string
Scanner sc = new Scanner(System.in);
System.out.print("Enter String");
String str= sc.nextLine();
String str1="";
for(int i=0 ;i<str.length();i++)
{
//Store one-one character from 'Str' to 'Str1' and replace it by space
//in the original string 'str'
if(str.charAt(i)!=' ')
{
str1=str1+str.charAt(i);
str=str.replace(str.charAt(i),' ');
}
}
//printing the final string removing all the duplicate characters
System.out.print(str1);
}
}

Comments
Post a Comment