Hello! I am new to this forum. I am beginner in Java programming.
I would like to get only unique characters in a string that means I would not have a repeating letters in my array.
For instance, if I have string "aaaabbbbccc", I only need to have 'a' 'b' and 'c' in my output
Moreover, the order of the letters in the string is to be respected. For instance, If my string is "znnmzzmnn", The output is 'z' followed by 'n' and 'm'.
This is just a part of the code I am doing in my project i.e Hufman coding. My code should read the string from a text file actually which I will do in the future after I solve this. My prof told us not to use subclasses and collections. I also wanted to ask if there is other way to do it aside from array especially different text file can have different amounts of letters
String line="abcdabcdefghij"; char []letter=line.toCharArray(); char []uniqueLetter=new char[10]; System.out.println("The unique letters in sequence are "); for(int i=0;i<uniqueLetter.length;i++) { for(int j=i;j<letter.length;j++) { if(uniqueLetter[i]!=letter[j]) uniqueLetter[i]=letter[j]; } System.out.println( "index "+i+": "+ uniqueLetter[i]); }
I am getting output:
#The unique letters in sequence are
index 0: j
index 1: j
index 2: j
index 3: j
index 4: j
index 5: j
index 6: j
index 7: j
index 8: j
index 9: j
Output I should be getting is:
#The unique letters in sequence are
index 0: a
index 1: b
index 2: i
index 3: c
index 4: d
index 5: e
index 6: f
index 7: g
index 8: h
index 9: j
Advanced Thanks!!!