First Non-Repeating Character Java | Fresher Interview
Fast recall
Quick revision
Core answer
int[256]); pass two scans the string left to right and returns the first character whose count is 1. O(n) time, O(k) space. The key insight: counting must finish before you look for the first unique, so a single pass alone can't answer it.Whether you use two passes (count, then scan in order) and preserve original order in the second pass
Trying to answer in one pass, or iterating the map (loses original order) instead of the string
Note that scanning the string (not the map) in pass two preserves order; mention LinkedHashMap as an alternative
Interview-ready guide
Interview answer
To find the first non-repeating character, I use two passes over the string.
In the first pass, I count how many times each character appears, using either a hash map from character to count or, since characters fit in a small range, a fixed integer array indexed by the character's code. In the second pass, I scan the string again from left to right and return the first character whose recorded count is exactly one.
The most important insight here is the ordering of the work: I can't know whether a character is unique until I've seen the entire string, so a single pass that tries to decide on the fly won't work. I have to complete all the counting first, then look for the first unique. The second important detail is what I iterate in the second pass.
I scan the original string, not the frequency map. A regular hash map has no defined order, so if I iterated the map looking for a count of one, I might return a unique character that isn't actually the first one in the string. Scanning the string preserves the left-to-right order the question asks for.
Both passes are linear, so the time complexity is O of n, and the space is O of k for the distinct characters, which is constant if the alphabet is fixed. As an alternative, I could use a LinkedHashMap, which preserves insertion order, so I could count into it and then iterate it directly and still get the first unique correctly, all with one data structure.
For example, swiss has counts s=3, w=1, and i=1. A second scan follows the original order, so w at index 1 is returned before i.
First Non-Repeating Character Java | Fresher Interview
Direct Answer
Two passes: pass one counts every character's frequency (HashMap or int[256]); pass two scans the string left to right and returns the first character whose count is 1. O(n) time, O(k) space. The key insight: counting must finish before you look for the first unique, so a single pass alone can't answer it.
Deep Dive & Architecture
The first non-repeating character is the earliest character whose total frequency is one. One pass cannot safely return the first new character, because a later occurrence may make it repeated.
Worked example: swiss
Count all characters first: s=3, w=1, and i=1. Scan the original string again. s is rejected because its count is 3; w is accepted at index 1. Although i is also unique, it appears later.
A HashMap<Character, Integer> works for general character values, but its iteration order must not be used to decide “first.” The second string scan preserves the true order. For lowercase English letters, an int[26] is a smaller alternative. If the input may contain emoji or supplementary Unicode characters, iterate code points instead of individual UTF-16 char units.
Both passes together are O(n), and storage is O(k) for k distinct characters. Return -1, an empty optional, or another clearly documented value when no unique character exists. Also state whether case and spaces count; A and a are different unless normalization is explicitly required.
Code Example
char firstUniq(String s) {
int[] count = new int[256];
for (char c : s.toCharArray()) count[c]++; // pass 1: count
for (char c : s.toCharArray()) // pass 2: scan IN ORDER
if (count[c] == 1) return c;
return '_'; // none unique
}
// s = "leetcode"
// counts: l=1,e=3,t=1,c=1,o=1,d=1
// pass 2 scans l(1) -> return 'l'
Common Mistakes & Pitfalls
- One-pass attempt: you can't know a char is unique until all are counted.
- Iterating the map: a
HashMaphas no order, so it may not return the first unique - scan the string. - Char range assumption:
int[256]covers ASCII; use a HashMap for Unicode. - Returning index vs character: follow the method's stated return contract.
Check your understanding
Answer these without looking back at the article.
- What is the frequency counting pattern and when do you apply it?
- How do you find the top K most frequent elements in an array?
- How do you group strings that are anagrams together?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.