First Non Repeating Character Using Hash in DSA Typescript - Time & Space Complexity
We want to know how the time to find the first non-repeating character changes as the input string gets longer.
How does the number of steps grow when the string size increases?
Analyze the time complexity of the following code snippet.
function firstNonRepeatingChar(s: string): string | null {
const charCount: Record = {};
for (const char of s) {
charCount[char] = (charCount[char] || 0) + 1;
}
for (const char of s) {
if (charCount[char] === 1) return char;
}
return null;
}
This code counts how many times each character appears, then finds the first character that appears only once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two loops over the string characters.
- How many times: Each loop runs once over all characters, so twice over the input length.
As the string gets longer, the number of steps grows roughly twice as fast because we scan the string two times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 steps |
| 100 | About 200 steps |
| 1000 | About 2000 steps |
Pattern observation: The steps grow linearly with input size, doubling because of two passes.
Time Complexity: O(n)
This means the time to find the first non-repeating character grows directly in proportion to the string length.
[X] Wrong: "Because there are two loops, the time complexity is O(n²)."
[OK] Correct: The loops run one after another, not nested. So the total steps add up, not multiply.
Understanding this helps you explain how to efficiently find unique elements using counting, a common skill in coding interviews.
"What if we used a nested loop to check each character against all others? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of a hash in frequency counting
A hash (dictionary/map) stores each character as a key and counts how many times it appears in the string.Step 2: Use frequency counts to find the first non-repeating character
By checking the counts in order, we can find the first character with count 1, which is the first non-repeating character.Final Answer:
To count the frequency of each character efficiently -> Option AQuick Check:
Hash counts characters = To count the frequency of each character efficiently [OK]
- Thinking hash sorts characters
- Trying to reverse string instead
- Assuming hash removes duplicates
Solution
Step 1: Identify correct TypeScript syntax for a map
TypeScript uses Map to create a hash map. Here, keys are strings and values are numbers.Step 2: Check each option for correctness
const charCount = new Map(); uses Map(), which is correct. Options A, B, and D use incorrect types or structures.Final Answer:
const charCount = new Map<string, number>(); -> Option CQuick Check:
Map<string, number> is correct syntax [OK]
- Using Array instead of Map
- Using Set which stores only keys
- Incorrect Object initialization
function firstNonRepeatingChar(s: string): string {
const count = new Map();
for (const ch of s) {
count.set(ch, (count.get(ch) ?? 0) + 1);
}
for (const ch of s) {
if (count.get(ch) === 1) return ch;
}
return '_';
}
console.log(firstNonRepeatingChar('swiss'));Solution
Step 1: Count character frequencies in 'swiss'
Characters: s(3), w(1), i(1). The map stores these counts.Step 2: Find first character with count 1 in order
Order: 's' (count 3), 'w' (count 1), 'i' (count 1). The first with count 1 is 'w'.Final Answer:
'w' -> Option DQuick Check:
First unique char in 'swiss' = 'w' [OK]
- Choosing first character without checking count
- Confusing 'i' and 'w'
- Returning '_' when unique char exists
function firstNonRepeatingChar(s: string): string {
const count = new Map();
for (let i = 0; i <= s.length; i++) {
const ch = s[i];
count.set(ch, (count.get(ch) ?? 0) + 1);
}
for (const ch of s) {
if (count.get(ch) === 1) return ch;
}
return '_';
}Solution
Step 1: Check the for loop boundary
The loop runs from i = 0 to i <= s.length, which accesses s[s.length], an undefined index causing error.Step 2: Correct loop condition
Change condition to i < s.length to avoid out-of-bounds access and correctly count characters.Final Answer:
Loop condition should be i < s.length, not i <= s.length -> Option BQuick Check:
Loop boundary off by one error [OK]
- Off-by-one error in loop condition
- Misusing Map initialization
- Wrong loop type for string iteration
'aabbccddeeffg', which modification to the hash-based approach will correctly return the first non-repeating character when some characters appear multiple times and some only once?Solution
Step 1: Count frequencies of all characters
Use a hash map to count how many times each character appears in the string.Step 2: Iterate string in original order to find first count 1
Check each character in order; the first with count 1 is the first non-repeating character.Final Answer:
After counting, iterate the string and return the first character with count 1 -> Option AQuick Check:
Count then check order for first unique char [OK]
- Sorting string loses original order
- Returning highest count instead of unique
- Ignoring character order in result
Ready to prove it?
Pass the test and this pattern moves unknown
