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


Post a Comment