Home  >  Article  >  Java  >  Find the First Non-Repeated Character in a String

Find the First Non-Repeated Character in a String

Patricia Arquette
Patricia ArquetteOriginal
2024-10-02 16:07:02769browse

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;
            }           
        }
    }
}


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!

Statement:
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