How to find the first non repeated character in string in java

This is quite famous question asked in Java interview . What does the question says that you have to find first non repeated character in string in java like for examples if you take a word missisippi the first non repeated character will be m.

or you can take an example of word stress , the non repeated first character is t . For this the approach we need to use is we'll traverse the string and store like how many times a particular character is there in string. This can be done with the help of HashMap  .So how the values will be stored is shown below :

< s , 3>
< t , 1>
< r, 1>
< e, 1>

Its like HashMap<key,Value > visual example (its not memory representation). So lets jump to the program .

The other way to do this program is to traverse the whole string characters one by one and count that particular character and at last if count is not more than 1 then just break the loop and this program will take less time to excute as the loop will break as soon as you find the first character .

But using HashMap we need to traverse the whole string and store the character count which ll take time.

package com.javainhouse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TestDemo

 public static void main(String[] args)
          String A = "abz2ab3cdeabfgcdeabeytvdecde";
          for (int i = 0; i < A.length(); i++) {
           int count =0;
           for (int j = 0; j < A.length(); j++) {
            System.out.println("the first non repeated character is "+A.charAt(i));



Java Program First Non Repeated Character


Thanks for sharing informative article on java application development. Your post helped to understand the career in Java.Java Training in Chennai


Thank you so much... your article is giving very useful knowledge for all.i didn’t have the knowledge in this now i get an idea about this.. thanks a lot:-)

dotnet Training in chennai


Hello Admin
Thanks for the sharing the articles was very helpful to me.
Cloud Computing training institute in Gurgaon | ethical hacking training in Gurgaon


This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
Android Training in Chennai
Ios Training in Chennai


Thanks for sharing this post on java. Very useful while attending interviews. Keep updating new codes.
Final Year Project Center in Chennai | IEEE Project Center in Chennai | Diploma Project Center in Chennai


Impressive blog with lovely information. really very useful article for us thanks for sharing such a wonderful blog... 
Software Testing Training Institute in Chennai | No.1 Software Testing Training with Placement in Chennai | Software Testing Training in Velachery


Impressive and Interesting post. This article exposes the ultimate use of language. Thanks for sharing.
Best Mat Lab Project Center in Chennai | IEEE Mat Lab Projects in Velachery


Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

Java Training in Chennai

Java Training in Bangalore


Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this. php training in chennai


You truly did more than visitors’ expectations. Thank you for rendering these helpful, trusted, edifying and also cool thoughts on the particular topic.
hadoop training in bangalore


I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.
java training in bangalore


I believe there are many more pleasurable opportunities ahead for individuals that looked at your site. hadoop training in bangalore


Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.


Informative blog and it was up to the point describing the information very effectively. Thanks to blog author for wonderful and informative post.
Mobile application developers in Chennai | Android application developers in Chennai | Android app
developers Chennai
| PHP developers chennai


Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

android training in bangalore


Post a Comment