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++) {
    
     if(A.charAt(j)==A.charAt(i))
     {
      count++;
     }
    }
          
           if(count==1)
           {
            System.out.println("the first non repeated character is "+A.charAt(i));
            break;
           }
   
   }
         
 
     }

}         
  

O/P

Java Program First Non Repeated Character



21 comments

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

Reply

hello!!!
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


Reply

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

Reply



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

Reply

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

Reply

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

Reply

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

Reply

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

Reply

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

Reply

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

Reply

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

Reply

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

Reply

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
data-science-training-in-bangalore

Reply

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

Reply

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

Reply

Hi, am a big follower of your blog. I am really happy to found such a helpful and fascinating post that is written in well manner.

white label website builder

mobile website builder

Reply

CIITN is the Best Php training institute in Noida and delhi Ncr. You will get Live Project Training on PHP by our PHP

expert who have 5+ year industrial experience.Focus on practical and live project training. In our PHP training, we you will learn core PHP, advance

PHP, HTML, CSS, JavaScript, jQuery, Bootstrap, Cake PHP and Wordpress.CIITN provides 100% job assistance in PHP training. CIITN is well known PHP

coaching center because our 100% PHP students are placed now.

Ciitnoida provides Core and java training institute in

noida
. We have a team of experienced Java professionals who help our students learn Java with the help of Live Base Projects. The object-

oriented, class-based build of Java has made it one of most popular programming languages and the demand of professionals with certification in

Advance Java training is at an all-time high not just in India but foreign countries too.

By helping our students understand the fundamentals and Advance concepts of Java, we prepare them for a successful programming career. With over 13

years of sound experience, we have successfully trained hundreds of students in Noida and have been able to turn ourselves into an institute for best

Java training in Noida.

java training institute in noida

Reply

Best Summer Internship In Noida

These technologies prepare individuals for fields like software programming, technical support, graphic design, software testing, business analytics. Embedded Systems, Industrial Automation Training and more. Candidates go through a series of comprehensive practical sessions where they work on live problems and implement solutions on real-time basis.
It has a dedicated placement cell which provides 100% placement assistance to students. The benefits that a student gets out of summer training are innumerable. CIITN,Best Summer training Center for B.Tech/CS/CSE/IT/ BCA/MCA/ B.E /M.tech / B.sc/ M.sc/ Engineering Student has already accomplished itself successfully in the field of Training and Development after setting milestones and bringing smiles to the faces of more than 1 Lakh students. CIITN was incorporated in the year 2002 and over the years CIITN has grown remarkably into the greatest training giant of Northern India.
There is no looking back to technology! Those passionate for it need not worry about the professional prospects it offers. Courses and trainings in the field of Computer Science and Applications open a wide array of choices for individuals. All you need to do it prepare yourself right!

Summer Internship Course In Noida

Summer Internship Training In Noida

Reply

The information you shared is very effective for learners.

Visit for a real time training for Java Training in Chennai | Best Java Training Institute in Chennai | Java Master Program Training in Chennai

Reply

Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep updating your blog. Java Training in Chennai

Reply

Post a Comment