Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Strings. 2 Comments on Java program to remove duplicate characters from a string

In this article we will learn to implement a Java program to remove duplicate characters from a string. A java program is provided below 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:

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 and output for the above program is as follows:

Input:
asaabebys

Output:
a
b
e
s
y

 

If you want to display in insertion order, you can use LinkedHashSet.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

2 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *