In this tutorial, we will learn java program to print all characters of the string which are not repeating.
Take any string as an input from the user and check if any character in the string is repeating or not if it is not repeating then print that character as output.
For example
Case1: If the user inputs the string ‘javaprogramming’
Then the output should be ‘javprogming’, where there is no character that is repeating.
Case2: If the user inputs the string ‘hitechpoints’
Then the output should be ‘ecpons’, where there is no character that is repeating.
Java Program to Print non repeating Characters in String
import java.util.*;
public class Main {
public static void main(String[] args) {
int max_count = 0;
char max_char = Character.MIN_VALUE;
System.out.println("Java program to find non Repeating Characters");
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a String");
String str = sc.nextLine();
int[] arr = new int[256];
for (int i = 0; i < str.length(); i++) {
arr[str.charAt(i)]++;
}
System.out.println("Non Repeating characters are");
for (int i = 0; i < 256; i++) {
if (arr[i] == 1) {
System.out.println((char) i);
}
}
}
}
Output
Java program to find non Repeating Characters
Please enter a String
Hitechpoints Website
Non Repeating characters are
H
W
b
c
h
n
o
p
Explanation:
For the input string ‘Hitechpoints Website’, as the characters ‘i’, ‘t’, ‘e’, and ‘s’,are repeating but ‘H’, ‘W’, ‘b’, ‘c’, ‘h’, ‘n’, ‘o’, and ‘p’ are not repeating.