Sunday, 22 January 2017

Write a Java Program to Find First Repeated And Non-Repeated Character from given String ?

Here we will learn how to find the first non repeated words from the string and first repeated word from the string. For example suppose we have string "teeter" in this string first non repeated word/character is "r" and first repeated word/character is "t"

Below program shows the logic to find the first non repeated word/character and first repeated word/character with multiple examples.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.LinkedHashMap;
import java.util.Map;

public class FirstNonRepeatedAndRepeatedCharacter {

 public static void getOutput(String str){
  Map<Character, Integer> map = new LinkedHashMap<>();
  for(char ch : str.toCharArray()){
   if(map.containsKey(ch)){
    map.put(ch, map.get(ch)+1);
   }
   else{
    map.put(ch, 1);
   }
  }
  for(char ch : str.toCharArray()){
   if(map.get(ch) == 1){
    System.out.println("First Non Repeated Character of String *"+str+"* is : "+ch);
    break;
   }
  }
  for(char ch : str.toCharArray()){
   if(map.get(ch) > 1){
    System.out.println("First Repeated Character of String *"+str+"* is : "+ch+"\n");
    break;
   }
  }
 }
 public static void main(String[] args) {
  FirstNonRepeatedAndRepeatedCharacter.getOutput("JavaConceptOfTheDay");
  FirstNonRepeatedAndRepeatedCharacter.getOutput("teeter");
  FirstNonRepeatedAndRepeatedCharacter.getOutput("stress");
 }
}

Output:

First Non Repeated Character of String *JavaConceptOfTheDay* is : J
First Repeated Character of String *JavaConceptOfTheDay* is : a

First Non Repeated Character of String *teeter* is : r
First Repeated Character of String *teeter* is : t

First Non Repeated Character of String *stress* is : t
First Repeated Character of String *stress* is : s


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



7 comments:

  1. I agree with your thoughts!!! As the demand of java programming application keeps on increasing, there is massive demand for java professionals in software development industries. Thus, taking training will assist students to be skilled java developers in leading MNCs.Best Java Training in Chennai | JAVATraining Institutes in Chennai

    ReplyDelete
  2. Terrific Blog Post. I like the way you explained these things. I’ve been looking for ways to improve my website and overall rankings.I hope your future article will help me further Software Testing Training in Chennai | Software Testing Training Institute in Chennai

    ReplyDelete
  3. Wonderful post. I learned so many things from your blog. keep updating...CCNP Training Institute in Chennai | CCNP Training Institute in Velachery

    ReplyDelete
  4. The information which you have provided is very good. It is very useful who is looking for
    Java online training Bangalore

    ReplyDelete
  5. https://studyinusainfo2u.blogspot.com/2011/08/100-pakkaguaranteed-i-20-universities.html?sc=1666441414279#c2877186795570274300

    ReplyDelete