2

I'm trying to delete duplicate characters from a string. For example if I enter the string abaqueru it should give me bqer with duplicate characters a and u deleted. However, instead the result is an unnecessary loop. Here is the code:

public class question {
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
 String s = "abaqueru";
 calculate(s);
 // TODO code application logic here
}
public static void calculate(String s){
 String result;
 for(int i = 0; i < s.length(); i++)
 {
 char c = s.charAt(i);
 char temp;
 temp=c;
 for(int j = 1; j < s.length(); j++)
 {
 char x = s.charAt(j);
 if(temp==x){
 s=s.replaceAll(""+temp,"");
 calculate(s);
 }
 }
 System.out.println(s);
 }
}
ReggieB
8,2673 gold badges42 silver badges48 bronze badges
asked Aug 13, 2015 at 13:58
2
  • don't change your question completely .ask new one if you have a another question Commented Aug 13, 2015 at 14:10
  • Sorry, i didnt know that. Commented Aug 13, 2015 at 14:14

3 Answers 3

7

String are immutable in java. After replaceall, you need to assign it back

s=s.replaceAll(""+temp,"");

If you didn't assign it back, the loop goes on forever.

That solves the stackoverflow error, I didn't concentrate on your actual logic :)

Jordi Castilla
27.1k8 gold badges73 silver badges113 bronze badges
answered Aug 13, 2015 at 14:00

1 Comment

I corrected it thanks but i think i have algorithm problems also that taking me unnecessarry loops.
2

I will go with Suresh Atta's Answer which was leading to stack overflow error. I also checked your logic. Have made some necessary changes

public class Question {
 /**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
 String s = "abaqueru";
 calculate(s);
 // TODO code application logic here
 }
 public static void calculate(String s){
 String result;
 for(int i = 0; i < s.length(); i++)
 {
 char c = s.charAt(i);
 char temp;
 temp=c;
 for(int j = i+1; j < s.length(); j++)
 {
 char x = s.charAt(j);
 if(temp==x){
 s=s.replaceAll(""+temp, "");
 break;
 // calculate(s);
 }
 }
 }
 System.out.println(s);
 }
}

However this can be further customized to achive performance in terms of space and time. Infact, more simpler logic can be used :)

Madhawa Priyashantha
9,8977 gold badges38 silver badges65 bronze badges
answered Aug 13, 2015 at 14:52

Comments

1

Definitely go with Suresh Atta's answer! However, I figured I'd help glance over your code to help you find where some of your mistakes were.

I just glanced over your logic in the code though, and there is definitely some stuff worth glancing over. The most notable is that j is never used in the inner-for-loop. In fact, if you are using an IDE, it should warn you that j is never used.

UPDATE: Also, consider using String.valueOf(char c) instead of the s=s.replaceAll(""+temp,""); call you have in the if(temp==x) condition. That is, I assume that is so you could cast the character to a String? Then you can use String.append(), which you can read about on the link I sent you!

answered Aug 13, 2015 at 14:07

4 Comments

Yes, In there if i found any letter duplication in string, I change that duplicated letter with ""(space). and i use // ""+temp // for string parse
If you don't use j in the inner for loop, all 3 character variables will be the same. Notice that you are grabbing the same character each time through the inner for loop, thus comparing it to a character it is guaranteed to be equal to. In other words, you are grabbing the character (at location i) j times.
Also, any particular reason why you went with a recursive solution this way?
I replace i with j in second loop and I dont know how many different repeated letter in string so i decide to use recursion.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.