Java Program to Find First Non Repeating Character
LinkedHashMap and then iterates to find the first character with count 1, like for (char c : input.toCharArray()) { if (map.get(c) == 1) return c; }.Examples
How to Think About It
Algorithm
Code
import java.util.LinkedHashMap; import java.util.Map; public class FirstNonRepeatingChar { public static char findFirstNonRepeatingChar(String str) { Map<Character, Integer> charCount = new LinkedHashMap<>(); for (char c : str.toCharArray()) { charCount.put(c, charCount.getOrDefault(c, 0) + 1); } for (char c : str.toCharArray()) { if (charCount.get(c) == 1) { return c; } } return '\0'; // null char if none found } public static void main(String[] args) { String input = "swiss"; char result = findFirstNonRepeatingChar(input); if (result != '\0') { System.out.println("First non repeating character: " + result); } else { System.out.println("No non repeating character found"); } } }
Dry Run
Let's trace the input "swiss" through the code
Count characters
Map after counting: {s=3, w=1, i=1}
Find first with count 1
Check 's' count=3 (skip), 'w' count=1 (return 'w')
| Character | Count |
|---|---|
| s | 3 |
| w | 1 |
| i | 1 |
Why This Works
Step 1: Counting characters
We use a LinkedHashMap to keep track of how many times each character appears, preserving order.
Step 2: Finding the first unique
We check characters in original order and pick the first with count exactly 1.
Step 3: Return result
If no character has count 1, we return a special value indicating no unique character.
Alternative Approaches
public class FirstNonRepeatingChar { public static char findFirstNonRepeatingChar(String str) { int[] counts = new int[256]; for (char c : str.toCharArray()) { counts[c]++; } for (char c : str.toCharArray()) { if (counts[c] == 1) { return c; } } return '\0'; } public static void main(String[] args) { String input = "swiss"; char result = findFirstNonRepeatingChar(input); if (result != '\0') { System.out.println("First non repeating character: " + result); } else { System.out.println("No non repeating character found"); } } }
public class FirstNonRepeatingChar { public static char findFirstNonRepeatingChar(String str) { for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); boolean repeated = false; for (int j = 0; j < str.length(); j++) { if (i != j && str.charAt(j) == c) { repeated = true; break; } } if (!repeated) { return c; } } return '\0'; } public static void main(String[] args) { String input = "swiss"; char result = findFirstNonRepeatingChar(input); if (result != '\0') { System.out.println("First non repeating character: " + result); } else { System.out.println("No non repeating character found"); } } }
Complexity: O(n) time, O(n) space
Time Complexity
The program loops through the string twice: once to count characters and once to find the first unique, so time is O(n).
Space Complexity
It uses extra space to store counts for each character, which can be up to O(n) in worst case.
Which Approach is Fastest?
Using a LinkedHashMap is efficient and preserves order; array counting is faster for ASCII but less flexible; brute force is slow and not recommended.
| Approach | Time | Space | Best For |
|---|---|---|---|
| LinkedHashMap counting | O(n) | O(n) | General strings with order preservation |
| Array counting (ASCII) | O(n) | O(1) | ASCII strings, faster but limited charset |
| Brute force two loops | O(n²) | O(1) | Very short strings or learning purpose |