Home > Algorithms > Remove duplicate characters in a string and display in the same order

Remove duplicate characters in a string and display in the same order

December 16, 2011

public class RemoveDuplicateChars
{
private String remove(String string)
{
int a[] = new int[256];
int length = string.length();
String removedString = new String();
for (int i = 0; i < length; i++)
{
char c = string.charAt(i);

if (a[c] == 0)
{
removedString = removedString + c;
a[c] = 1;
}
}
return removedString;

}

public static void main(String[] args)
{
System.out.println(new RemoveDuplicateChars().remove("vidyasagar"));
}
}

The above problem can be solved in many number of ways.This is my approach.pleases let me know if you
have any other better approach

Advertisements
Categories: Algorithms
%d bloggers like this: