- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to Find the first non-repeating character from a stream of characters
To find the first non-repeating character from a stream of characters, the Java code is as follows −
Example
import java.util.ArrayList; import java.util.List; public class Demo{ final static int max_chars = 256; static void non_repeating_char(){ List<Character> my_list = new ArrayList<Character>(); boolean[] repeat = new boolean[max_chars]; String my_str = "Thisisasample"; for (int i = 0; i < my_str.length(); i++){ char x = my_str.charAt(i); if (!repeat[x]){ if (!(my_list.contains(x))){ my_list.add(x); } else{ my_list.remove((Character)x); repeat[x] = true; } } if (my_list.size() != 0){ System.out.print("The first non-repeating character of the string is "); System.out.println(my_list.get(0)); } } } public static void main(String[] args){ non_repeating_char(); } }
Output
The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T
A class named Demo contains a function named ‘non_repeating_char’ function. A list is created and a string is defined. This string is iterated over, and every character is inspected, and its count is stored in the form of a Boolean variable, in an array named ‘repeat’. The value will be true if it is repeated and false otherwise. In the main function, the function is called, and relevant message is displayed on the console.
- Related Articles
- Python program to Find the first non-repeating character from a stream of characters?
- Find the first non-repeating character from a stream of characters in Python
- Finding first non-repeating character JavaScript
- Finding the first non-repeating character of a string in JavaScript
- How to find its first non-repeating character in a given string in android?
- Write a program to find the first non-repeating number in an integer array using Java?
- Find first repeating character using JavaScript
- First non-repeating character using one traversal of string in C++
- Find the last non repeating character in string in C++
- Queries to find the last non-repeating character in the sub-string of a given string in C++
- Return index of first repeating character in a string - JavaScript
- Finding the index of the first repeating character in a string in JavaScript
- First non-repeating in a linked list in C++
- Character Stream vs Byte Stream in Java
- Java Program to retrieve a Stream from a List

Advertisements