You are here : Home / Core Java Tutorials / Interview Programs (beginner to advanced) in java / Level1 programs for (beginner)
Example 1 in java>
s="aabcdeefggi";
/*
//Output
NonRepeatableCharacter = b
NonRepeatableCharacter = c
NonRepeatableCharacter = d
NonRepeatableCharacter = f
Last NonRepeatableCharacter = i
*/
Program/ example >
//String Find The First Non Repeatable Character using core java - don't use HashMap
public class StringFindTheFirstNonRepeatableCharacter {
public static void main(String[] args) {
String s= "zzzzzbbbccccddehhhhiii";
s="aabc";
s="aabcdeefggi";
char ch[]=s.toCharArray();
char currentCh=ch[0];
int currentChCount=1;
for(int i=1; i<ch.length;i++){ //start from 1st pos, not oth
if(currentCh == ch[i]){ //Means its repeated // Ex - a after a
currentChCount++;
}
else if(currentChCount==1){ //Means it was NonRepeatableCharacter // Ex - c after b
System.out.println("NonRepeatableCharacter = "+currentCh);
currentCh=ch[i];
currentChCount=1;
}
else{ //currentChCount>1 //It was a repeated Ex - b after aa
currentCh=ch[i];
currentChCount=1;
}
}
if(currentChCount==1){//Check for last character
System.out.println("Last NonRepeatableCharacter = "+currentCh);
}
}
}
/*
//Output
NonRepeatableCharacter = b
NonRepeatableCharacter = c
NonRepeatableCharacter = d
NonRepeatableCharacter = f
Last NonRepeatableCharacter = i
*/
|
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
Related >
Labels:
Core Java
Level1 programs (beginners)