Hello! So I’m doing a 30 day coding challenge where I solve around 3-5 questions per day and thought of posting them here on my blog so that you guys can join the challenge too!
Welcome to Coding challenge Day 9: Problem 1! Be sure to post your answers, queries etc in the comments!
Problem: Find first non-repeating character in String
Sample input: Monday
Output: The first non-repeated character in string Monday is M
Sample input: fluffy
Output: The first non-repeated character in string fluffy is l
Solutions:
package string;
public class Program008print_first_non_repeating_character {
public static void main (String [] args){
String str= "DDda";
int length= str.length();
char count[] = new char[256];
for(int i=0; i<length; i++){
count[str.charAt(i)]++;
}
for(int i=0; i<length; i++){
if(count[str.charAt(i)]==1){
System.out.println("The first non-repeated character in string "+ str + " is "+ str.charAt(i));
break; }
}
}
}
Happy Learning!!