beginner · strings · frequency count · hashing
The first letter that never repeats
Problem
A log line is written in lowercase letters. Return the position of the first letter that occurs exactly once in the whole line, or −1 if every letter occurs at least twice.
Positions start at 0. "Exactly once" refers to the whole line, not only to the part before the position.
Constraints
- 0 ≤ length ≤ 30 (visualization limit)
- letters a–z only
Examples
"swiss" → 1 — s occurs three times; w at position 1 occurs once.
"abab" → -1
Show the solution
Count, then scan
Uniqueness is a property of the whole line. After one pass of counting, it becomes a constant-time lookup per position.
Why it is correct
After the first pass, counts[x] is the number of occurrences of x in the line. The second pass visits positions in increasing order and returns the first one whose letter has count 1, which is by definition the answer; if none qualifies, −1 is correct.
Complexity
Time O(n), extra space O(1) — 26 counters, independent of n. Two passes of n constant-time steps. The counter array has a fixed size of 26, so it does not grow with the input.
For contrast: For each position, count its letter across the whole line. That costs O(n²) time and O(1) space.
Edge cases
- Empty line: −1.
- One letter: position 0.
- Every letter repeated: −1.
- The unique letter is the last one.
Common mistakes
- Returning the first letter that has not been seen before (that is the first new letter, not the first unique one).
- Scanning the counter array in alphabetical order instead of the line in position order.
Step through it
The solution in four languages
Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.
namespace Bekadan.Dsa.Examples.Problems;
public static class FirstUniqueChar
{
/// <summary>Position of the first letter (a-z) that occurs exactly once in the text, or -1. O(n) time, O(1) space.</summary>
public static int Find(string text)
{
var counts = new int[26];
foreach (char ch in text) counts[ch - 'a']++;
for (int i = 0; i < text.Length; i++)
{
if (counts[text[i] - 'a'] == 1)
return i;
}
return -1;
}
}
Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/FirstUniqueChar.cs
def find(text: str) -> int:
"""Position of the first letter (a-z) that occurs exactly once in the text, or -1. O(n) time, O(1) space."""
counts = [0] * 26
for ch in text:
counts[ord(ch) - ord("a")] += 1
for i, ch in enumerate(text):
if counts[ord(ch) - ord("a")] == 1:
return i
return -1
Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/first_unique_char.py
package dev.bekadan.dsa.problems;
public final class FirstUniqueChar {
private FirstUniqueChar() {
}
/** Position of the first letter (a-z) that occurs exactly once in the text, or -1. O(n) time, O(1) space. */
public static int find(String text) {
int[] counts = new int[26];
for (int i = 0; i < text.length(); i++) {
counts[text.charAt(i) - 'a']++;
}
for (int i = 0; i < text.length(); i++) {
if (counts[text.charAt(i) - 'a'] == 1) {
return i;
}
}
return -1;
}
}
Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/FirstUniqueChar.java
#pragma once
#include <array>
#include <string_view>
namespace bekadan::dsa::problems {
// Position of the first letter (a-z) that occurs exactly once in the text, or -1. O(n) time, O(1) space.
inline int first_unique_char(std::string_view text) {
std::array<int, 26> counts{};
for (const char ch : text) ++counts[static_cast<std::size_t>(ch - 'a')];
for (std::size_t i = 0; i < text.size(); ++i) {
if (counts[static_cast<std::size_t>(text[i] - 'a')] == 1) {
return static_cast<int>(i);
}
}
return -1;
}
} // namespace bekadan::dsa::problems
Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/first_unique_char.hpp