Wednesday, 8 January 2014

Java: Remove duplicate elements in a string

Given a string *Str of ASCII characters, write the pseudo code to remove the duplicate elements present in them. For example, if the given string is "Potato", then, the output has to be "Pota". Additional constraint is, the algorithm has to be in-place( no extra data structures allowed).

public class HelloWorld{

     public static void main(String []args){
         String str="potato";
        int[] A = new int[256];//maximum value of an ASCII character
        StringBuffer sb = new StringBuffer();

        int len = str.length();
        for (int i = 0; i < len; i++)
            char c = str.charAt(i);
            if (A[c] == 0)
                A[c] = 1;


No comments:

Post a Comment