Coding Challenge Day-9: Problem 1: Find first non-repeating character in String

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;			}
			
		}
		
	}

}

Download Code

Happy Learning!!

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close