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



22 comments

This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.

I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.

Microsoft Azure Admin Training in Chennai
Microsoft Azure Admin Training

Reply

Post a Comment