write a program to find non repeat character from a string in java?

To find first non repeat character. there are perform following step : –

  • Here we are using two for loop to iterate the data.
  • In first for loop we iterate the data from 0 to last index value from s1.length() method and set status true.
  • The length() method returns the number of characters present in the string.
  • From second for loop we again iterate data from 0 to last index value.
  • Inside if condition we compare both the character value from charAt() method and checking first index value not equal to second value.
  • Now set status false and break the loop.
  • If status is true then from charAt() method we print character at depend on index.

				
					public class NonRepeatChar {
	public static void main(String[] args) {
		String s1 = "abcabcpabsder";
		boolean status = false;
		for (int i = 0; i < s1.length(); i++) {
			status = true;
			for (int j = 0; j < s1.length(); j++) {
				if (i != j && s1.charAt(i) == s1.charAt(j)) {
					status = false;
					break;
				}
			}
			if (status) {
				System.out.println("non repeat char:– " + s1.charAt(i));
			}
		}
	}
}
				
			

Output :-
non repeat char:– p
non repeat char:– s
non repeat char:– d
non repeat char:– e
non repeat char:– r

Leave a Comment

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