如何在没有任何内置函数的情况下从java中的两个字符串中删除重复的字母?

时间:2014-05-21 12:51:02

标签: java string

这里我有两个字符串。从那里,我如何删除常见的字母/字符并存储在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]);
            }
        }
    }
%>

预期结果是:
g l e o u t s c k v f w

4 个答案:

答案 0 :(得分:3)

获得独特字母的最快方法可以是:

String firstString = "google out";
String secondString = "stack overflow";

Set<String> output = new HashSet<String>();
Collections.addAll(output, (firstString + secondString).split(""));

输出(包含在开头有一个空格的排序字母,因为它也是唯一字符):

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

答案 1 :(得分:3)

这是一种方法,

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

输出

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

修改

如果您的预期输出不正确,并且您实际上想要出现在两个字符串中的字符。

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

哪个输出

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

修改2

如果您真的想要与此相反,请将调用更改为contains并添加第二个循环(对于反向关系) -

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

然后输出

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

答案 2 :(得分:0)

这是另一种方法:

private String getUnicChars(String first, String second) {
            String result = "";
            for (int i = 0; i < first.length(); i++) {
                if (!second.contains(String.valueOf(first.charAt(i)))) {
                    result = result + first.charAt(i);
                }
            }

            return result;
        }


  public String yourMethod(String firstString, String secondString) {            
        return getUnicChars(firstString, secondString) + getUnicChars(secondString, firstString);
  }

然后你拨打yourMethod("google out", "stack overflow");

[Actualization]我假设您只是要求检查两个字符串之间的redondant char,而不是在一个String中。在我的解决方案中,&#39; g&#39; char将被给予两次。[/ actualization]

答案 3 :(得分:0)

public class RoughWork2 {

    // function to remove duplicates first
    public static String removeDuplicate(String val) {

        char[] chars = val.toCharArray();
        Set<Character> charSet = new LinkedHashSet<Character>();
        for (char c : chars) {
            charSet.add(c);
        }

        StringBuilder sb = new StringBuilder();
        for (Character character : charSet) {
            sb.append(character);
        }
        return sb.toString();
    }

    public boolean sort(String a, String b) {

        String arg1 = removeDuplicate(a);
        String arg2 = removeDuplicate(b);
        if (arg1.length() != arg2.length()) {
            System.out.print("two strings count are not equal");

            return false;
        } else {
            for (int x = 0; x < arg1.length(); x++) {
                int c = (int) arg1.charAt(x);
                for (int y = 0; y < arg2.length(); y++) {
                    int d = (int) arg2.charAt(y);
                    if (c == d) {

                        System.out.print(Character.toChars(c));
                    } else {
                        System.out.print("*");
                    }
                }
                System.out.println();
            }
            return true;
        }

    }

    public static void main(String[] args) {

        RoughWork2 rw = new RoughWork2();

        rw.sort("hello", "hallo");

    }
}