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.

5
  • 2
    You can't operate on a String without using the methods inside the String class, can you? Commented Oct 23, 2015 at 9:34
  • seems like an interview question.. Commented Oct 23, 2015 at 9:35
  • Pass it to a wrapper class & use that wrapper class' methods instead. Commented Oct 23, 2015 at 9:37
  • @Tunaki Yes ... Its not possible with out using String method... Commented Oct 23, 2015 at 9:39
  • Possible duplicate of String length without using length() method Commented Oct 23, 2015 at 11:43

6 Answers 6

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
Sign up to request clarification or add additional context in comments.

4 Comments

How does this remove the duplicate characters?
@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.
You're still using String methods, even thoug they aren't as obvious as in OPs suggestion :P.
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..:)
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! ;-)

1 Comment

Yes. This is absolutely naughty
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);

Comments

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);

Comments

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

Comments

0

You can use slice() function to remove the character from the left of the string.

var string= 'testautomation';

var stringfinal=string.slice(4);

cy.log(stringfinal)

Output: automation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.