Java program to find first non repeated character in a string

Java program to find first non repeated character in a string

Write a Java program to find first non repeated character in a string is one of the frequently asked coding interview questions for experience of 0-2 years. Here we will see four methods to find first non repeated character in a string.

Approach#1: Using LinkedHashMap

Note that you should use LinkedHashMap since the normal HashMap may not guarantee the insertion order.

Approach#2: Using index comparison

Approach#3: Using character comparison

Approach#4: Sorting and then adjacent character comparison.

In this approach, first we will sort the given string and then compare the adjacent characters alone.

The programs might look simple but it is important to remember the logic so that it will become easy to write it during an interview. Also it is important to be familiar with multiple methods of solving a problem since sometimes the interviewer may trick you to write the program with some condition, for example write the program without using LinkedHashMap or write without using indexOf() method.

Hope you find the post useful.

Also read my posts on Frequently asked Java interview questions with answers.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.
One comment

Add Comment

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