Harsha Bharadwaz
Harsha Bharadwaz

Reputation: 21

Getting square boxes as output

I'm trying to print the distinct characters of a string in alphabetical order which is the result of concatenation of other two strings. I tried but I am getting small square boxes as an output. Here is my code:

String s1 = "xyaabbbccccdefww", s2 = "xxxxyyyyabklmopq";
String s = s1+s2;
char[] c = s.toCharArray();
java.util.Arrays.sort(c);
char[] res = new char[c.length];
res[0]=c[0];
for(int i = 0; i<c.length ; i++) {
  boolean isDuplicate=false;
  for(int j = 0 ; j<c.length; j++) {
    if(i!=j && c[i]==c[j]) {
      isDuplicate=true;
      break;
    }
  }
  if(!isDuplicate) {
    res[i+1]=c[i];
  }
}
System.out.println(String.valueOf(res));

I'm getting an output like this:

Image of output

But I want an output like:

abcdefklmopqwxy

Upvotes: 0

Views: 1307

Answers (2)

Lajos Arpad
Lajos Arpad

Reputation: 76884

This is how you can achieve what you wanted:

String getDistinctCharacters(String input) {
    String output = "";
    for (int i = 0; i < input.length(); i++) {
        if (output.indexOf(input.charAt(i)) < 0) {
            if ((output.length() == 0) || (output.charAt(0) > input.charAt(i))) {
                output = input.charAt(i) + output;
            } else {
                boolean found = false;
                for (int j = 0; (!found) && (j < output.length()); j++) {
                    if (output.charAt(j) < input.charAt(i)) {
                        found = true;
                        output = output.substring(0, j) + input.charAt(i) + output.substring(j + 1);
                    }
                }
                if (!found) output += input.charAt(i);
            }
        }
    }
    return output;
}

Upvotes: 0

curlyBraces
curlyBraces

Reputation: 1105

The reason you are getting boxes in your result is because you are assigning some char into your res array in only certain indexes, depending on the value of i when !isDuplicate condition is true.

Also, there's a bug in the logic to detect duplicate characters. See the correction below. Instead of a char array, you can use a StringBuilder to store your result as following:

String s1 = "xyaabbbccccdefww", s2 = "xxxxyyyyabklmopq";
String s = s1+s2;
char[] c = s.toCharArray();
java.util.Arrays.sort(c);
StringBuilder result = new StringBuilder();
for(int i = 0; i<c.length ; i++) {
    boolean isDuplicate=false;
    for(int j = i+1 ; j<c.length; j++) {
        if(c[i]==c[j]) {
            isDuplicate=true;
            break;
        }
    }
    if(!isDuplicate) {
        result.append(c[i]);
    }
}
System.out.println(result.toString());

Solution with only char array:

String s1 = "xyaabbbccccdefww", s2 = "xxxxyyyyabklmopq";
String s = s1+s2;
char[] c = s.toCharArray();
java.util.Arrays.sort(c);
char[] result = new char[c.length];
int resultIndex = 0;
for(int i = 0; i<c.length ; i++) {
    boolean isDuplicate=false;
    for(int j = i+1 ; j<c.length; j++) {
        if(c[i]==c[j]) {
            isDuplicate=true;
            break;
        }
    }
    if(!isDuplicate) {
        result[resultIndex++]=c[i];
    }
}
char[] actualResult = new char[resultIndex];
for(int i=0;i<resultIndex;i++) {
    actualResult[i] = result[i];
}
System.out.println(String.valueOf(c));
System.out.println(String.valueOf(result));
System.out.println(String.valueOf(actualResult));

Upvotes: 2

Related Questions