Reputation: 1
Imagine I have this:
String input = "AB BC"; // ( Just an Example)
I want to put this String into an char array, but i want to have no duplicates and blank symbols in my char Array. My solution so far:
String input = "AB BC"
char array[]=input.toCharArray();
for(int i=0;i<array.length;i++){
System.out.println("Data at ["+i+"]="+array[i]);
}
The Output is :
This is my input String AB BC
This is the content of my Char Array
Data at [0]=A
Data at [1]=B
Data at [2]=
Data at [3]=
Data at [4]=B
Data at [5]=C
Data at [6]=
So now I don't know how I can erase the duplicates and the blank symbols.
Upvotes: 0
Views: 1018
Reputation: 6527
Simply you can try this:
String input = "AB BC";
char array[]=input.toCharArray();
for(int i=0;i<array.length;i++){
if(!input.substring(0,i).contains(array[i]+"") && array[i]!=' ')
System.out.println("Data at ["+i+"]="+array[i]);
}
Output:
Data at [0]=A
Data at [1]=B
Data at [5]=C
Upvotes: 0
Reputation: 3718
this will add only the chars without any blanks to the hashset
char array[]=input.toCharArray();
Set<Character> m=new LinkedHashSet<Character>();
for(int i=0;i<array.length;i++){
if(array[i]!=' ')
m.add(array[i])
}
Character[] text = m.toArray(new Character[0]);
System.out.println(Arrays.toString(text))
Upvotes: 0
Reputation: 16067
You can use a LinkedHashSet<Character>
(to maintain insertion order).
replaceAll
method on your String object to replace whitespacesSet
(a Set doesn't allow duplicates)toArray(T[] object)
method to get back a Character
array.So it would be something like this :
String input = "AB BC";
input = input.replaceAll("\\s+", "");
Set<Character> s = new LinkedHashSet<>();
for(char c : input.toCharArray())
s.add(c);
Character [] array = s.toArray(new Character[0]);
System.out.println(Arrays.toString(array));
Output :
[A, B, C]
If you want to have back an array of primitive you can use (note that you have to use the apache commons library) char[] arr = ArrayUtils.toPrimitive(array);
Here's the source code :
2647 public static char[] toPrimitive(Character[] array) {
2646 if (array == null) {
2647 return null;
2648 } else if (array.length == 0) {
2649 return EMPTY_CHAR_ARRAY;
2650 }
2651 final char[] result = new char[array.length];
2652 for (int i = 0; i < array.length; i++) {
2653 result[i] = array[i].charValue();
2654 }
2655 return result;
2656 }
Upvotes: 1
Reputation: 533930
You could use a LinkedHashSet but I assume you want an array at the end. You can do this.
String input = ...
StringBuilder sb = new StringBuilder();
BitSet seen = new BitSet(); // more compact than a HashSet<Character>
seen.set(' '); // pretend we have seen space already
for(char ch: input.toCharArray()) {
if(!seen.get(ch)) {
sb.append(ch);
seen.set(ch);
}
}
char[] unique = sb.toString().toCharArray();
Upvotes: 0
Reputation: 12040
use Hashset
of generic type character
HashSet<Character> m=new HashSet<Character>();
Upvotes: 0
Reputation: 1252
Transfer content to LinkedHashSet
. It will remove the duplicates for you !
Here's an example to start with.
Upvotes: 1