easyString ProblemsJava

Find the First Non-Repeating Character in a String

Return the first character in a string that appears exactly once.

Quick Answer

First count how many times each character appears using an order-preserving map. Then scan the map (or the string) in order and return the first character whose count is exactly one. If every character repeats, report that none exists. Two passes give O(n) time.

Problem Statement

Given a string, find the first character that does not repeat — that is, the leftmost character that appears exactly once in the whole string. For swiss the answer is w, because s and i repeat.

If every character repeats and no unique character exists, report that clearly (for example, return none).

Input: A single string s.

Output: The first non-repeating character, or none if there isn't one.

Examples

Example 1
Input:  "swiss"
Output: w

s and i repeat; w is the first character with a count of one.

Example 2
Input:  "aabb"
Output: none

Both a and b appear twice, so there is no non-repeating character.

Constraints

  • 0 <= s.length <= 10^5
  • Return the first such character in left-to-right order

Think Before You Code

Reveal the questions to ask yourself first
  • How do you know how many times each character appears?
  • Why is a single pass not enough to know something is unique?
  • How do you keep track of which character came first?
  • What should you return when nothing qualifies?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
You cannot tell a character is unique until you have seen the whole string, so plan on two passes.
Hint 2
First pass: build a count of every character. Second pass: look for the first count equal to one.
Hint 3
A LinkedHashMap preserves first-appearance order, so scanning its entries returns the leftmost unique character directly.

Approach

Reveal the step-by-step approach

Use two passes: one to count, one to find.

  1. Build a LinkedHashMap<Character, Integer> of character counts in first-seen order.
  2. Iterate the map's entries in order.
  3. Return the first character whose count is exactly 1.
  4. If no entry has a count of 1, return none.

Because the map preserves insertion order, the first entry with count 1 is also the leftmost unique character in the original string.

Dry Run

Walk through the example step by step

Finding the first non-repeating character in "swiss":

pass 1 (counts, in first-seen order): {s=3, w=1, i=1}

pass 2 (scan entries in order):
entry | count | count == 1?
------+-------+------------
s=3   | 3     | no
w=1   | 1     | yes -> return "w"

Solution

Reveal the full Java solution
import java.util.LinkedHashMap;
import java.util.Map;

public class FirstNonRepeating {
    public static String firstNonRepeating(String s) {
        Map<Character, Integer> freq = new LinkedHashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            freq.put(c, freq.getOrDefault(c, 0) + 1);
        }
        for (Map.Entry<Character, Integer> entry : freq.entrySet()) {
            if (entry.getValue() == 1) {
                return String.valueOf(entry.getKey());
            }
        }
        return "none";
    }

    public static void main(String[] args) {
        System.out.println(firstNonRepeating("swiss")); // w
        System.out.println(firstNonRepeating("aabb"));  // none
    }
}

The first pass records how often each character occurs. Uniqueness cannot be decided while still reading the string, which is why the check happens in a second pass. Because the LinkedHashMap keeps characters in the order they first appeared, the first entry with a count of 1 is exactly the leftmost non-repeating character. If the loop finds none, the method returns none.

Time: O(n) where n is the string lengthSpace: O(k) where k is the number of distinct characters

Common Mistakes

  • Trying to decide uniqueness in one pass before the full string is seen.
  • Using a plain HashMap, which loses the first-appearance order.
  • Forgetting the case where every character repeats and no answer exists.

Edge Cases to Test

  • An empty string has no non-repeating character (return none).
  • A single character is always non-repeating ("z" returns z).
  • A string where all characters repeat, like "aabb", returns none.

Interview Follow-Ups

  • How would you return the index of the first non-repeating character instead?
  • How would you solve it with a fixed-size int[256] array instead of a map?
  • How would you find the first non-repeating character in a stream of characters?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us