Programming Examples
Java program to remove duplicate character from string
Java program to remove duplicate character from string
Sample:
Input:Â infomaxcomputeracademy
Output:Â Â infomaxcputerdy
Solution
import java.util.*;
class RemoveDuplicate
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Any String");
String mystr=sc.nextLine();
char str[] = mystr.toCharArray();
int n = str.length;
int index = 0;
for (int i = 0; i < n; i++)
{
int j;
for (j = 0; j < i; j++)
{
if (str[i] == str[j])
{
break;
}
}
if (j == i)
{
str[index++] = str[i];
}
}
System.out.println(String.valueOf(Arrays.copyOf(str, index)));
}
}
Output