tajMahal tajMahal - 1 month ago 8
Java Question

How to remove duplicate alphabets from two strings in java without any builtin functions?

Here I have two strings. From that, how can I remove common alphabets/characters and store in result(third) String?

<%
String[] firstString = {"google out"};
String[] secondString = {"stack overflow"};
String[] result={""};
for (int i = 0,k=0; i < firstString.length; i++,k++) {
for (int j = 0; j < secondString.length; j++) {
if (firstString[i].equalsIgnoreCase(secondString[j])) {
} else {
result[k]=result[j]+firstString[i];
out.println(result[k]);
}
}
}


%>
expected result is:
g l e o u t s c k v f w

Answer

Here is one approach,

// Write a static helper method.
public static boolean contains(char[] in, int index, char t) {
    for (int i = 0; i < index; i++) {
        if (in[i] == t) return true;
    }
    return false;
}

public static void main(String[] args) {
    String firstString = "google out"; // String(s) not String arrays
    String secondString = "stack overflow";
    // output cannot be larger then the sum of the inputs.
    char[] out = new char[firstString.length() + secondString.length()];
    int index = 0;
    // Add all unique chars from firstString
    for (char c : firstString.toCharArray()) {
        if (! contains(out, index, c)) {
            out[index++] = c;
        }
    }
    // Add all unique chars from secondString
    for (char c : secondString.toCharArray()) {
        if (! contains(out, index, c)) {
            out[index++] = c;
        }
    }
    // Create a correctly sized output array.
    char[] s = new char[index];
    for (int i = 0; i < index; i++) {
        s[i] = out[i];
    }
    // Just print it.
    System.out.println(Arrays.toString(s));
}

Output is

[g, o, l, e,  , u, t, s, a, c, k, v, r, f, w]

Edit

If your expected output is incorrect, and you actually want the characters that appear in both Strings.

public static void main(String[] args) {
    String firstString = "google out";
    String secondString = "stack overflow";
    char[] out = new char[firstString.length() + secondString.length()];
    int index = 0;
    for (char c : firstString.toCharArray()) {
        if (contains(secondString.toCharArray(), secondString.length(), c)) {
            out[index++] = c;
        }
    }
    char[] s = new char[index];
    for (int i = 0; i < index; i++) {
        s[i] = out[i];
    }
    System.out.println(Arrays.toString(s));
}

Which outputs

[o, o, l, e,  , o, t]

Edit 2

If you actually wanted the opposite of that, change the call to contains and add a second loop (for the inverse relationship) -

    for (char c : firstString.toCharArray()) {
        if (! contains(secondString.toCharArray(), secondString.length(), c)) {
            out[index++] = c;
        }
    }
    for (char c : secondString.toCharArray()) {
        if (! contains(firstString.toCharArray(), firstString.length(), c)) {
            out[index++] = c;
        }
    }

Which will then output

[g, g, u, s, a, c, k, v, r, f, w]
Comments