This is a Java program that accepts a string from the user, ignores any duplicate characters and prints each character only once in ascending order.
We use TreeSet class from collections framework for printing the characters in ascending order.
The program is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.*; public class Sample { public static void main(String[] args) { TreeSet<Character> list = new TreeSet<Character>(); Scanner input = new Scanner(System.in); String s = input.next(); for(char ch : s.toCharArray()) list.add(ch); for(char ch : list) System.out.println(ch); } } Input: asaabebys Output: a b e s y |
If you want to display in insertion order, you can use LinkedHashSet.
Heya i am for the first time here. I came across this board and I
find It really useful & it helped me out a lot.
I hope to give something back and aid others like you aided me.
Hurrah, that’s what I was looking for, what a stuff!
existing here at this weblog, thanks admin of this site.