Logo

dev-resources.site

for different kinds of informations.

Find the First Non-Repeated Character in a String

Published at
10/2/2024
Categories
java
string
interview
Author
realnamehidden1_61
Categories
3 categories in total
java
open
string
open
interview
open
Author
18 person written this
realnamehidden1_61
open
Find the First Non-Repeated Character in a String

Problem:

Given a string, find the first character that does not repeat.

Example:

Input: "swiss"
Output: 'w'

Hint:

Use a LinkedHashMap to store the frequency of each character while maintaining the insertion order. Then, iterate over the map to find the first character with a count of 1.

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


Enter fullscreen mode Exit fullscreen mode
string Article's
30 articles in total
Favicon
Leetcode β€” 2942. Find Words Containing Character
Favicon
Python day-28 Dictionary, Frequency of character using nested loops
Favicon
Python Day-19 csv file,String methods,ASCII,Task
Favicon
Redis Cache - A String story
Favicon
Python Day-22 String Functions logic using loops, Recursion, Tasks
Favicon
Greatest Common Divisor of Strings in Javascript
Favicon
Day 22- String Functions and Recursion
Favicon
Day 21- String Functions
Favicon
Python Day-21 String functions logic using loops
Favicon
Python Day-20 String functions logic using loops,Task
Favicon
Day 20 - String functions
Favicon
Day 19 - CSV file, ASCII, String methods
Favicon
Python Day 6- String Functions,Looping-For,ifelse conditions and Task
Favicon
Python Day 5 - String functions
Favicon
Mastering String Operations in JavaScript πŸš€
Favicon
String Data Structures and Algorithms: Essential Interview Questions
Favicon
Find the First Non-Repeated Character in a String
Favicon
Longest substring without repeating characters
Favicon
Create JS function to remove spaces from giving string. ( Using core js and not in-built trim function.)
Favicon
Write a function that removes duplicate characters from a given string. ( Try to write core JS)
Favicon
Knuth Morris Prat algorithm[Pattern Matching]
Favicon
The JS string replace() method
Favicon
Pergunte ao especialista - Strings
Favicon
Strings
Favicon
C# {String Methods}
Favicon
Extending String for Validation in Dart 3
Favicon
String and Trailing comma, get couple and become, Tuple (): A copy & paste mistake to Error and concept
Favicon
Top 10 String Javascript Interview Coding Question
Favicon
Bash string manipulation
Favicon
C++ ηš„ string η‰©δ»Άεˆ°εΊ•δ½”εΉΎε€‹δ½ε…ƒη΅„οΌŸ

Featured ones: