How to get the first non-repeating character in a string in java
For example: input name and output n, input teeter and output r, input namename and output null
The specific implementation code is as follows:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.next(); for(int i =0 ; i < str.length() ; i++) { if(str.lastIndexOf(str.char(i)) == i && str.indexOf(str.char(i)) == i) { System.out.println(str.char(i)); break; } } } }
Online Video tutorial recommendation: java video
In this implementation code we use three method members of the String class:
String.length( )
: Get the length of the string
String.charAt(int index)
: Get the character of the index index
String.lastIndexOf(char c )
: Get the index of the last time character c appears in the string
String.indexOf(char c)
: Get the index of the first time character c appears in the string
In fact, we can also implement these methods without using strings. The following is the code that I personally use for loop to implement:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.next(); char[] cb = new char[str.length()]; //将字符串中的字符一次存入cb[] for(int i =0 ;i <str.length() ; i++) { cb[i] = str.charAt(i); } for(int i = 0 ; i < str.length() ; i++) { for(int j = 0 ; j < str.length() ; j++) { if(cb[i] == cb[j] && cb[i] != '0') { char c = cb[i]; for(int z = 0 ; z < str.length() ; z++) { if(cb[z] == c) cb[z] = '0'; } } } } for(int i = 0 ; i <str.length() ; i++) { if(cb[i] != '0') { System.out.println(cb[i]); break; } } } }
This method can be implemented, but this method The time complexity is extremely large, and the system overhead is also extremely large. Therefore, it is best not to use loop nesting unless it is absolutely necessary, otherwise the system overhead will be very large.
Recommended related articles and tutorials: java quick start
The above is the detailed content of How to get the first non-repeating character in a string in java. For more information, please follow other related articles on the PHP Chinese website!

The article discusses various Java garbage collection algorithms (Serial, Parallel, CMS, G1, ZGC), their performance impacts, and suitability for applications with large heaps.

The article discusses the Java Virtual Machine (JVM), detailing its role in running Java programs across different platforms. It explains the JVM's internal processes, key components, memory management, garbage collection, and performance optimizatio

Java's Nashorn engine enables JavaScript scripting within Java apps. Key steps include setting up Nashorn, managing scripts, and optimizing performance. Main issues involve security, memory management, and future compatibility due to Nashorn's deprec

Java's try-with-resources simplifies resource management by automatically closing resources like file streams or database connections, improving code readability and maintainability.

Java enums represent fixed sets of values, offering type safety, readability, and additional functionality through custom methods and constructors. They enhance code organization and can be used in switch statements for efficient value handling.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools