Reputation: 418
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
Upvotes: 3
Views: 3835
Reputation: 535
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");
}
}
Upvotes: 0
Reputation: 201437
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]
Upvotes: 3
Reputation: 1461
Here is an other method:
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);
}
Then you call yourMethod("google out", "stack overflow");
.
[Actualisation] I assume that you just ask to check redondant char between the two strings but not inside one String. In my solution, the 'g' char will be given twice.[/actualisation]
Upvotes: 0
Reputation: 152216
The fastest way to get unique letters can be:
String firstString = "google out";
String secondString = "stack overflow";
Set<String> output = new HashSet<String>();
Collections.addAll(output, (firstString + secondString).split(""));
Output (contains sorted letters with one whitespace in the beginning because it's also unique character):
[ , a, c, e, f, g, k, l, o, r, s, t, u, v, w]
Upvotes: 3