1

can any one suggest me how to remove duplicate characters from a string in java and too without using string functions.

As i know we convert a given string to character array and then we can use the TreeSet to remove the duplicates. But ToCharArray is again a string function.

raj
  • 41
  • 8

5 Answers5

1

You could create a Scanner which reads that string as if you where reading it through a stream.

    String input = "Hello World";
    Scanner scanner = new Scanner(input);
    scanner.useDelimiter("");
    while(scanner.hasNext()) {
        System.out.println(scanner.next());
    }

Yields:

H
e
l
l
o

W
o
r
l
d
npinti
  • 51,780
  • 5
  • 72
  • 96
  • 1
    How does this remove the duplicate characters? – Tunaki Oct 23 '15 at 09:40
  • 1
    @Tunaki: The OP seems to know how to remove duplicate information since he explicetely states the usage of sets. What the problem appears to be is that the OP needs to find a way to get the characters from a string without using `.toCharArray()` method, which my answer provides. – npinti Oct 23 '15 at 09:57
  • You're still using String methods, even thoug they aren't as obvious as in OPs suggestion :P. – Tom Oct 23 '15 at 10:07
  • Here is the complete solution: String input = "Hello World"; LinkedHashMap lmp=newLinkedHashMap(); Scanner scanner = new Scanner(input); scanner.useDelimiter(""); while(scanner.hasNext()) { lmp.put(scanner.next()); } System.out.print(lmp.keySet()); Thank you npinti..:) – raj Oct 24 '15 at 09:27
1

All the other answers are bypassing the use of String methods by using other classes, such as StringBuilder or Scanner. However, they are still using String methods, albeit indirectly, because these other classes themselves use String methods. My solution is a bit "naughty" and jdk-specific, but at least it doesn't use any String methods at all:

    String s = "hello";
    Class<String> c = (Class<String>) Class.forName("java.lang.String");
    Field f = c.getDeclaredField("value");
    f.setAccessible(true);
    char[] array = (char[]) f.get(s);

... and then do your processing entirely on the array.

See, no String methods used! ;-)

Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70
0

charAt and length are from Interface CharSequence

So go with the StringBuilder and use the it's implementation of those too methods.

StringBuilder sb = new StringBuilder("My My My");

And using

sb.length()

in combination with

sb.chartAt(index);

you can iterate over your stringbuilder identify the duplicates, and remove them using

sb.deleteCharAt(index_of_char_to_delete);
Tahar Bakir
  • 716
  • 5
  • 14
0

How about this?

    String str = "eyyigwdqweqe";
    StringBuilder builder = new StringBuilder(str);
    HashSet<String> mySet = new HashSet<String>();

    for(int i = 0; i<builder.length();i++)
        mySet.add(builder.charAt(i)+"");
    System.out.println(mySet);
Jude Niroshan
  • 4,280
  • 8
  • 40
  • 62
0
public class RemoveDuplicates {

    public static void main(String[] args) {

        String input = new String("Geeksforgeeks");
        String output = new String();
        boolean isPresent;

        if (input.length()==0){
            System.out.println("Input String Empty. Program Terminated");
        }
        else {
            //read each character of input string 
            for (int i = 0; i < input.length(); i++) {
                isPresent=false;

                //read through entire output string 
                for (int j = 0; j < output.length(); j++) {

                    //check if character exists in output string then set flag
                    if (Character.toLowerCase(input.charAt(i)) == Character.toLowerCase(output.charAt(j))) {
                        isPresent = true;
                        break;
                    }
                }//for              
                //if flag is false means character is not dduplicated then add to output string
                if (!isPresent) {
                    output += input.charAt(i);
                }//if               
        }//end of input string loop         
        System.out.println("Original input : "+input);
        System.out.println("Final output   : "+output);
        }
    }//main
}//class
chevybow
  • 9,959
  • 6
  • 24
  • 39
Jayashree
  • 1
  • 1