I'm facing a problem right now. In one of my program, I need to remove strings with same characters from an Array. For eg. suppose,
I have 3 Arrays like,
String[] name1 = {"amy", "jose", "jeremy", "alice", "patrick"};
String[] name2 = {"alan", "may", "jeremy", "helen", "alexi"};
String[] name3 = {"adel", "aron", "amy", "james", "yam"};
As you can see, there is a String amy
in the name1
array. Also, I have strings like may
, amy
and yam
in the next two arrays. What I need is that, I need a final array which does not contain these repeated Strings. I just need only one occurrence: I need to remove all permutations of a name in the final array. That is the final array should be:
String[] finalArray={"amy", "jose", "alice", "patrick","alan", "jeremy", "helen", "alexi","adel", "aron", "james"}
(The above array removed both yam, may, and only includes amy).
What i have tried so far, using HashSet
, is as below
String[] name1 = {"Amy", "Jose", "Jeremy", "Alice", "Patrick"};
String[] name2 = {"Alan", "mAy", "Jeremy", "Helen", "Alexi"};
String[] name3 = {"Adel", "Aaron", "Amy", "James", "Alice"};
Set<String> letter = new HashSet<String>();
for (int i = 0; i < name1.length; i++) {
letter.add(name1[i]);
}
for (int j = 0; j < name2.length; j++) {
letter.add(name2[j]);
}
for (int k = 0; k < name3.length; k++) {
letter.add(name3[k]);
}
System.out.println(letter.size() + " letters must be sent to: " + letter);
But, the problem with this code is that, it just removes multiple occurences of the same string. Is there any other alternative? Any help is very much appreciated.