Home > Java > javaTutorial > body text

Find the First Non-Repeated Character in a String

Patricia Arquette
Release: 2024-10-02 16:07:02
Original
768 people have browsed it

Find the First Non-Repeated Character in a String

Java Code

import java.util.LinkedHashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {

        String s = "swiss";
        LinkedHashMap<Character,Integer> hm = new LinkedHashMap<>();
        for(int i=0;i<s.length();i++) {
            hm.put(s.charAt(i), hm.getOrDefault(s.charAt(i), 0)+1);
        }
        for(Map.Entry<Character, Integer> e : hm.entrySet()) {
            if(e.getValue() == 1) {
                System.out.println(e.getKey());
                break;
            }           
        }
    }
}


Copy after login

The above is the detailed content of Find the First Non-Repeated Character in a String. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!