Challenge - 5 Problems
Hash Mastery: First Non Repeating Character
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediateOutput of First Non Repeating Character Index
What is the output of the following JavaScript code that finds the index of the first non-repeating character in a string?
DSA Javascript
function firstNonRepeatingCharIndex(s) {
const charCount = {};
for (const ch of s) {
charCount[ch] = (charCount[ch] || 0) + 1;
}
for (let i = 0; i < s.length; i++) {
if (charCount[s[i]] === 1) return i;
}
return -1;
}
console.log(firstNonRepeatingCharIndex("swiss"));Attempts:
2 left
๐ก Hint
Check the count of each character and find the first with count 1.
โ Incorrect
The string "swiss" has characters: s(3), w(1), i(1). The first non-repeating character is 'w' at index 1. The code returns 1.
โ Predict Output
intermediateOutput of First Non Repeating Character Index with all repeating
What is the output of the following JavaScript code when all characters repeat?
DSA Javascript
function firstNonRepeatingCharIndex(s) {
const charCount = {};
for (const ch of s) {
charCount[ch] = (charCount[ch] || 0) + 1;
}
for (let i = 0; i < s.length; i++) {
if (charCount[s[i]] === 1) return i;
}
return -1;
}
console.log(firstNonRepeatingCharIndex("aabbcc"));Attempts:
2 left
๐ก Hint
If no character appears once, the function returns -1.
โ Incorrect
All characters in "aabbcc" appear twice, so no non-repeating character exists. The function returns -1.
๐ง Debug
advancedIdentify the error in this First Non Repeating Character code
What error does the following code produce when run?
DSA Javascript
function firstNonRepeatingCharIndex(s) {
const charCount = new Map();
for (const ch of s) {
if (charCount.has(ch)) {
charCount.set(ch, charCount.get(ch) + 1);
} else {
charCount.set(ch, 1);
}
}
for (let i = 0; i < s.length; i++) {
if (charCount.get(s[i]) === 1) return i;
}
return -1;
}
console.log(firstNonRepeatingCharIndex("hello"));Attempts:
2 left
๐ก Hint
Check how Map methods are used and how properties are assigned.
โ Incorrect
The code mixes Map methods with object property assignment. `charCount[ch] = 1` sets an own property on the Map instance, not a Map entry. `Map.has(ch)` and `Map.get(ch)` only check Map entries, which are never set, so `get(s[i])` returns undefined !== 1 for all, returns -1. No runtime error.
โ Predict Output
advancedOutput of First Non Repeating Character with Unicode characters
What is the output of the following code when the input string contains Unicode characters?
DSA Javascript
function firstNonRepeatingCharIndex(s) {
const charCount = {};
for (const ch of s) {
charCount[ch] = (charCount[ch] || 0) + 1;
}
for (let i = 0; i < s.length; i++) {
if (charCount[s[i]] === 1) return i;
}
return -1;
}
console.log(firstNonRepeatingCharIndex("aรกa"));Attempts:
2 left
๐ก Hint
Unicode characters are treated as distinct keys in the hash.
โ Incorrect
The string "aรกa" has 'a' twice and 'รก' once at index 1. So the first non-repeating character is at index 1.
๐ Application
expertHow many unique characters are counted in this string?
Using the hash counting method, how many unique characters does the string "abcabcdeff" have?
DSA Javascript
function countUniqueChars(s) {
const charCount = {};
for (const ch of s) {
charCount[ch] = (charCount[ch] || 0) + 1;
}
return Object.keys(charCount).length;
}
console.log(countUniqueChars("abcabcdeff"));Attempts:
2 left
๐ก Hint
Count distinct characters in the string.
โ Incorrect
The string "abcabcdeff" has unique characters: a, b, c, d, e, f (6 unique).
